我讀了所有關於這個問題的相關帖子的插入行,但我仍然有一個錯誤:不能在表視圖
'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
@Vanya什麼東西? –
每次插入或刪除tableView中的行時,都應該更新數據源。檢查這個答案的更多細節:http://stackoverflow.com/a/40072874/1689376 – alexburtnik