ios - UITableView插入行无效更新

标签 ios swift uitableview

我曾在指定的索引路径上删除和重新加载行,但从未插入过。这是我正在尝试做的事情:

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/

相关文章:

ios - 如何设置tableViewIndex的自定义宽度?

swift - 从 TableView 中的单元格中删除突出显示的文本

Android - 将我的 UITableView 移植到 android

arrays - 变量行的索引路径

ios - 添加 JSON 数据时 UITableView 不工作

ios - NSURLCache 问题 - 图像从缓存中释放?

c++ - 编译外部 C++ 库以用于 iOS 项目

ios - 如何在表格 View 单元格中快速处理多个文本字段

ios - UINavigationControllerDelegate 不起作用

ios - prepareForSegue 将数据发送到容器 View 中的 TableView