当我试图删除UITableView部分时,EXC_BAD_ACCESS

时间:2016-07-12 12:09:56

标签: ios iphone swift uitableview

我有这段代码:

    // - Update the data source
    self.order.persons.removeAtIndex(index)

    // - Disable the user interaction
    self.tableView.userInteractionEnabled = false

    // - Delete the needed section from the table view
    let sectionIndexSet = NSIndexSet(index: self.order.persons.count - 1)
    self.tableView.beginUpdates()
    self.tableView.deleteSections(sectionIndexSet, withRowAnimation: .Automatic)
    self.tableView.endUpdates()

    // - Update the table view with a delay to update all the indexes 
    NSTimer.scheduledTimerWithTimeInterval(0.3, handler: {
            self.tableView.userInteractionEnabled = true
            self.tableView.reloadData()
        })

所以问题是:

  • 案例1:当在表格视图的可见区域上是另一个部分,不仅是我要删除的部分,一切都很好。

  • 案例2:当在表视图的可见区域上只有我要删除的部分时,调试器停在该行:

    self.tableView.endUpdates()

并给出错误:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0).

我不明白为什么案例1工作得很好,但案例2没有。有关信息,我使用可重复使用的单元格。

1 个答案:

答案 0 :(得分:0)

您正在从源和tableView中删除不同的数据

<强>问题

 // - Delete the needed section from the table view
    let sectionIndexSet = NSIndexSet(index: self.order.persons.count - 1)

<强> FIX 它应该是

// - Delete the needed section from the table view
    let sectionIndexSet = NSIndexSet(index: index)