来自 ViewController 的 UITableView reloadData(),扩展名为 UITableViewDataSource、UITableViewDelegate

UITableView reloadData() from ViewController with extension of UITableViewDataSource, UITableViewDelegate

我使用 table 视图扩展 ViewController。

如何使用表中的 reloadData()ViewController?

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
    }  
}

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

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

将数据解析为数据后