来自ViewController的UITableView reloadData(),具有UITableViewDataSource,UITableViewDelegate的扩展名

时间:2018-07-08 19:13:20

标签: ios swift uitableview reloaddata

我在表视图中使用了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
    }  
}

1 个答案:

答案 0 :(得分:0)

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

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

将数据解析为数据后

相关问题