我有一个 uitableview
,我尝试通过 insertRowsAtIndexPaths
函数附加它。
如果我只是将数据添加到数据源数组,uitableview
会很好地追加,但让我很难通过上述函数插入它。
这是我的代码
func insertData(){
// appending arrays
answersdict.append(answersmain[answertitle1]![0]) // count 1
resultanswers2d.append(carsmain[answertitle1]!) // count 3
self.tableView.reloadData()
// inserting rows
self.tableView.beginUpdates()
let insertedIndexPathRange = 0..<carsmain[answertitle1]!.count + 1 // total count 4
var insertedIndexPaths = insertedIndexPathRange.map { NSIndexPath(forRow: $0, inSection: 1) }
self.tableView.insertRowsAtIndexPaths(insertedIndexPaths, withRowAnimation: .Fade)
self.tableView.endUpdates()
我的部分数据源计数是 2(所以第 1 部分存在)并且对于 numberOfRows
- 它等于 resultanswers2d[section].count + 1
这给了我 4
所以我试图将 4 行插入到已经附加的数组中,该数组在该部分中有 4 行可能,但我不断收到类似
的错误The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus inserted cells (4 inserted, 0 deleted)
或者如果我努力尝试
malloc_error
卡住了。需要任何见解。谢谢。
最佳答案
删除行以重新加载表格。
insertRowsAtIndexPaths
相应地更新表格。
数据源和表中的插入次数和位置必须匹配。
关于ios - InsertRowsAtIndexPaths 错误 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32377962/