【发布时间】:2016-11-12 10:20:55
【问题描述】:
我阅读了有关此问题的所有相关帖子,但仍然遇到错误:
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (13) must be equal to the number of rows contained in that section before the update (13), plus or minus the number of rows inserted or deleted from that section (11 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
代码如下:
func appendItems(entities: [Entity]) {
if entities.count > 0 {
let entitesFirstPos = items.count
self.items.append(contentsOf: entities)
var indexPaths: [IndexPath] = []
for i in entitesFirstPos..<items.count {
indexPaths.append(IndexPath(row: i, section: 0))
}
self.tableView.beginUpdates()
self.tableView.insertRows(at: indexPaths, with: .none)
self.tableView.endUpdates()
}
}
【问题讨论】:
-
我认为 UITableView 的委托方法是做这些事情的更好地方。
-
@Vanya 有什么用?
-
您还应该在每次在 tableView 中插入或删除行时更新您的数据源。检查此答案以获取更多详细信息:stackoverflow.com/a/40072874/1689376
-
基本上你应该确保
numberOfSections和numberOfRows方法返回更新的值。
标签: ios swift uitableview swift3