EXC_BAD_ACCESS当我试图删除一个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:当在表视图的可见区域上只有我想删除的部分时,debugging器停在行:

    self.tableView.endUpdates()

并给出了错误:

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

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

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

问题

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

修正它应该是

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