简体   繁体   English

InsertRowsAtIndexPaths错误迅速

[英]InsertRowsAtIndexPaths error Swift

I have an uitableview and i try to append it by insertRowsAtIndexPaths function. 我有一个uitableview ,我尝试通过insertRowsAtIndexPaths函数附加它。 The uitableview appends nicely if i simply add data to the data source arrays, but gives me hard time inserting it by the mentioned above function. 如果我只是简单地将数据添加到数据源数组中,则uitableview很好地附加,但是让我很难通过上述功能将其插入。

Here is my code 这是我的代码

    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()

My datasource count for sections is 2 (so section 1 exists) and for numberOfRows - its equal to resultanswers2d[section].count + 1 which gives me 4 我的数据源计数为2(因此第1节存在)和numberOfRows等于resultanswers2d[section].count + 1等于4

So i'm trying to insert 4 rows into the already appended array that has 4 possible rows in that section, but i'm constantly getting errors like 所以我试图将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) 更新(4)之后,现有节中包含的行数必须等于更新(4)之前该节中包含的行数,加上或减去插入的单元格(已插入4个,已删除0个)

or if i try to hard 或者如果我努力

malloc_error malloc_error

Got stuck. 被困。 Need any insights. 需要任何见解。 Thank you. 谢谢。

Delete the line to reload the table. 删除该行以重新加载表格。

insertRowsAtIndexPaths updates the table accordingly. insertRowsAtIndexPaths更新表。

The number of insertions and the positions in data source and table must match. 插入次数以及数据源和表中的位置必须匹配。

Simply don't call self.tableView.reloadData() . 只是不要调用self.tableView.reloadData() The beginUpdate and endUpdate sections should suffice. beginUpdate和endUpdate部分应足够。

UPDATE: Also, remember to insert the new section too in the beginUpdate endUpdate block. 更新:另外,请记住也将新部分插入 beginUpdate endUpdate块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM