TableView 错误:“尝试将第 1 行插入第 0 节,但更新后第 0 节中只有 0 行”

问题描述 投票:0回答:1

enter image description here

func reloadCellOfTableView(_ tableView: UITableView, cell: UITableViewCell) {
    DispatchQueue.main.async {
        if let indexPath = tableView.indexPath(for: cell) {
            tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        }
    }
}

我调用上面的函数后,应用程序崩溃了,firebase上的错误日志是:

尝试将第1行插入第0节,但更新后第0节中只有0行。

此错误很少发生。

我尝试使用

tableView.beginUpdates()
tableView.endUpdates()

swift tableview
1个回答
0
投票

在处理 UITableView 时,始终保持数据源和表视图之间的同步状态至关重要。如果数据源可以在后台线程中更改,请谨慎并优先考虑同步。

为确保一致性,请在向数据源添加新数据后立即调用beginUpdate/insert/endUpdate。 UITableViews 缓存发生变化,可以在 CPU 时间和资源充足时实现更高效的更新。

调用 endUpdates 后,UITableView 会查询数据源以获取更新的节数和行数。如果数据源直接链接到节数和行数,请确保新计数(包括任何插入和删除)准确匹配数据源的数字。

最后,请注意:避免将“reloadData”调用与 beginUpdate/endUpdate 对混合在一起。坚持使用其中之一以确保顺利更新并防止潜在问题。

© www.soinside.com 2019 - 2024. All rights reserved.