我正在尝试同时删除行、删除部分和重新加载行。我做了一个简单的应用程序来演示我的崩溃。它从 3 个部分开始。当buttonPressed
被调用时,节 0 和 1 将从 中删除data
,然后performBatchUpdates
被调用以更新表视图。
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
var data: [[String]] = [
["0-0"],
["1-0"],
["2-0",
"2-1",
"2-2"],
]
@IBAction func buttonPressed(_ sender: UIButton) {
data = [
["2-0 new",
"2-1 new",
"2-2 new"],
]
tableView.performBatchUpdates({
self.tableView.deleteRows(at: [
IndexPath(row: 0, section: 0),
IndexPath(row: 0, section: 1),
], with: .automatic)
self.tableView.reloadRows(at: [
IndexPath(row: 0, section: 2),
IndexPath(row: 1, section: 2),
IndexPath(row: 2, section: 2),
], with: .automatic)
self.tableView.deleteSections(IndexSet([0, 1]), with: .automatic)
}, completion: nil)
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = data[indexPath.section][indexPath.row]
return cell
}
}
我收到以下行/部分会计异常,但我不明白其中一些数字来自哪里:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新 (3) 后现有节中包含的行数必须等于该节中包含的行数更新前的节 (3),加上或减去从该节插入或删除的行数(2 插入,3 删除),加上或减去移入或移出该节的行数(0 移入,0 移动出去)。'