首页 > 解决方案 > 来自 ViewController 的 UITableView reloadData(),扩展名为 UITableViewDataSource、UITableViewDelegate

问题描述

我使用 ViewController 的扩展来处理我的表格视图。

如何在 TableViewController 中使用 reloadData()?

extension ViewController: UITableViewDataSource, UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return articles.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let a = articles[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell", for: indexPath) as! ArticleTableViewCell
        cell.titleLabel.text = a.title

        return cell
    }  
}

标签: iosswiftuitableviewreloaddata

解决方案


首先,如果数据是静态的,意味着您在代码中定义的数据从哪里进入文章数组,那么您只需为 tableView 创建一个出口并 self.tableView.reloadData在 viewDidLoad 中使用,如果您的数据来自 api,那么您可以使用此代码

     ` DispatchQueue.main.async {
            self.tableview.reloadData()
        }`

将数据解析为数据后


推荐阅读