ios - 来自 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
}
}
解决方案
首先,如果数据是静态的,意味着您在代码中定义的数据从哪里进入文章数组,那么您只需为 tableView 创建一个出口并 self.tableView.reloadData
在 viewDidLoad 中使用,如果您的数据来自 api,那么您可以使用此代码
` DispatchQueue.main.async {
self.tableview.reloadData()
}`
将数据解析为数据后
推荐阅读
- javascript - 重新渲染组件的问题
- data-structures - 招聘问题 - 方法可视化问题?
- java - SonarLint 抛出 IllegalStateException -> 无法读取本地问题存储索引
- reactjs - 如何在反应中从列表中迭代?
- javascript - npm replace:用 npm 脚本替换多个文件中的文本的问题
- .net - 在 .NET Core 中添加自定义 BasicAuthenticationHandler - 将必要的服务注入处理程序的问题
- spring - 实体数据加载_in Spring Batch Boot
- c++ - 如何在参数化的 googletest 中传递 N 个变量进行测试?
- xaml - Xamarin Forms Right center 复选框
- c - C - 无法在 OSX 的当前目录上创建文件