ios - 离开并返回视图后 TableView 无法正常工作
问题描述
如果您离开他们的视图并回来,我对 tableView 的行为感到困惑。
我有一个带有一个 tableView 的屏幕,当我第一次进入视图时它可以工作。添加、删除和更新表格单元格有效。但是,当我按下一个按钮进入下一个视图并立即返回时,它tableView
不再起作用。应该执行的代码(tableView.reload()
以及所有相关的方法)按应有的方式运行。但是,即使在内部更新了数组,屏幕也不会更新,并且重新加载会运行并执行应该更新屏幕的代码(也就是说,tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
运行得很好)。
我错过了什么?如果我离开视图并返回到 ti,tableView 是否需要任何特殊处理?
谢谢。
编辑:
tableView 类的代码类似于:
class DebugViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var table: UITableView!
var array = [M]()
override func viewDidLoad() {
super.viewDidLoad()
self.searchbar.delegate = self
self.table.delegate = self
self.table.dataSource = self
search_view = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Search_view") as? SomeViewController
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellManage") as? TableCellManage else { return UITableViewCell() }
let idx = indexPath.row
let value = array[idx]
cell.lbl_time.text = value.month
cell.lbl_background.text = value.color
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 130
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count
}
@IBAction func tapped_addsongs(_ sender: Any) {
self.present( search_view, animated: true, completion: nil)
}
}
解决方案
tableView.reload()部分在哪里?由于您尝试在后台线程中更新表,因此可能会生成此问题。所有 UI 更新必须在主线程中完成:
func someFunc() {
...
DispatchQueue.main.async {
tableView.reload()
}
...
}
推荐阅读
- flutter - 时间前具有特定时间格式
- flutter - Flutter:有没有办法在不阻塞主隔离的情况下异步构建复杂的小部件?
- django - 我们如何在 Django 中使用多种用户类型
- python-3.x - Python:当从包含 pandas 数据帧数据的字符串变量中获取 unicode 字符时,re.sub 会给出不同的输出
- amazon-web-services - 将一个域指向或重定向到另一个域
- reactjs - 在 Firebase onAuthStateChanged 填充状态导致再次登录页面之前重定向到私有路由
- javascript - 使用vue js显示一个循环的聊天列表卡片
- excel - 从给定网站抓取数据时出错
- function - Spark Window 功能,这是 Spark 中的错误吗?
- node.js - 在 POST API 请求有效负载中附加文件