我曾在指定的索引路径上删除和重新加载行,但从未插入过。这是我正在尝试做的事情:
func reloadTableView(newIndexPaths: [IndexPath]) {
print(dataSource.numberOfItems()) // prints 44
print(newIndexPaths.count) // prints 22
tableView.beginUpdates()
tableView.insertRows(at: newIndexPaths, with: .none)
tableView.endUpdates() // Crashes before this line
}
这是我收到的崩溃:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (44) must be equal to the number of rows contained in that section before the update (22), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
没有意义的是,它告诉我只尝试插入 1 个项目,但实际上我实际上插入了 22 个项目。我的数据源已正确更新,为什么这不起作用?
最佳答案
@Paulw11 是的!这正是问题所在,我不敢相信我没有意识到这一点。当我创建索引路径时,我在循环创建过程时过早地忘记了增加行值。谢谢!
关于ios - UITableView插入行无效更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42143721/