ios - InsertRowsAtIndexPaths 错误 swift

标签 ios swift uitableview

我有一个 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/

相关文章:

iphone - Apple 是否允许监控跳板事件?

ios - 如何使用效果音频单元?

ios - 根据星期几显示不同的消息

ios - 将数据从 TableViewCell 中的 IBAction 发送到 TableViewViewController

ios - 重新加载 UItableview 未更新标签

ios - semaphore_wait_trap 阻塞 UI

ios - 将从 UIPicker 中选择的值分配给 Swift 中的 UITextField(以及如何显示和隐藏它)

ios - JSON 到核心数据 SQLITE 错误

swift - iOS9、Swift、SpriteKit 游戏中的 Admob 插页式广告 fatal error

iphone - [self.tableView reloadData] 处的 EXC_BAD_ACCESS