1. 程式人生 > >Swift reason: 'attempt to insert row 10 into section 0, but there are only 10 rows in section

Swift reason: 'attempt to insert row 10 into section 0, but there are only 10 rows in section

如果用的時NSArray或者NSDictionary來標識SectionRow數量,請在呼叫增刪方法時更新好資料。並且在beginUpdate enUpdate方法中間增刪操作。

self.tableView.beginUpdates()

//陣列元素+1

self.lists.append(self.lists.count+1)

//底部插入一行資料

let bottomIndexPath = IndexPath.init(row: self.lists.count-1, section: 0)

let insertIndexPaths = Array.init(arrayLiteral: bottomIndexPath)

//同樣,將資料加到資料列表後重新整理

self.tableView.insertRows(at: insertIndexPaths, with: .automatic)

self.tableView.endUpdates()

//滾動位置

self.tableView.scrollToRow(at: bottomIndexPath, at: .bottom, animated: true)