如何在按下按钮时显示数组数据中的 iOS tableview - Swift

How to display iOS tableview from array data on button press - Swift

我有一个数组,我正试图用它来填充 table按下按钮(导航栏按钮)时的视图。我已经使用打印语句来验证是否存在正确的数据,但我无法在按下按钮时显示 table 行。

我假设问题出在我的 @IBAction func favoritesButton(_ sender: UIBarButtonItem) 语法中,但现在确定如何。我错过了什么?

class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!

    @IBOutlet weak var tableView: UITableView!

    var materialData = ["One", "Two", "Three", "Four", "Five"]

    override func viewDidLoad() {
        super.viewDidLoad()

        print(favoritesData)

    }

    @IBAction func arrayList(_ sender: UIBarButtonItem) {
        print(favoritesData)
    }

    @IBAction func favoritesButton(_ sender: UIBarButtonItem) {
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.numberOfLines = 0
            cell.textLabel?.text = favoritesData[indexPath.row]
            print(favoritesData)
            return cell
        }
    }
}

var searchMaterial = [String]()
var searching = false
var favoritesData = [String]()

extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print(self.materialData[indexPath.row], "selected!")
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

        let favorite = UITableViewRowAction(style: .default, title: "Favorite") { (action, indexPath) in

            var data: String

            if searching {
                data = searchMaterial[indexPath.row]
                print(searchMaterial[indexPath.row], "added to favorites")
            } else {
                data = self.materialData[indexPath.row]
                print(self.materialData[indexPath.row], "added to favorites")
            }
            if let index = favoritesData.firstIndex(of: data) {
                favoritesData.remove(at: index)
            }
            else {
                favoritesData.append(data)
            }

        }
        favorite.backgroundColor = UIColor.orange
        return [favorite]
    }

}

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return searchMaterial.count
        } else {
            return materialData.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.numberOfLines = 0
        if searching {
            cell.textLabel?.text = searchMaterial[indexPath.row]
        } else {
            cell.textLabel?.text = materialData[indexPath.row]
        }
        return cell
    }

}

extension ViewController: UISearchBarDelegate {

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchBar.setShowsCancelButton(true, animated: true)
        searchMaterial = materialData.filter({[=12=].prefix(searchText.count) == searchText})
        searching = true
        tableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.setShowsCancelButton(false, animated: true)
        searching = false
        searchBar.text = ""
        tableView.reloadData()
        tableView.endEditing(true)
    }

}


不要在按钮操作中写 cellForRow .. cellForRowAtIndexPath 是您的 dataSource 提供给 UITableView 的方法。它由 iOS 调用以加载特定 indexPath 的单元格。它的工作原理是 dequeueing 一个可重复使用的单元格并填写该单元格的信息。

你通常不会调用它。 iOS 这样做

在您的按钮操作中,只需像在您的案例中那样设置您的数据源 favoritesData,例如 favoritesData = data,然后调用 tableView.reloadData()