EXC_BAD_ACCESS when I'm trying to delete a UITableView sections

心已入冬 提交于 2020-01-17 04:44:11

问题


I have this code:

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

So the problem:

  • CASE 1: When on the visible zone of table view are another sections, not only the one that I want to delete, all works great.

  • CASE 2: When on the visible zone of table view is ONLY the section that I want to delete, the debugger stop at the line:

    self.tableView.endUpdates()

and gives the errors :

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

I don't understand why case 1 is working great, but case 2 not. For information, I use reusable cells.


回答1:


You are removing different data from source and in tableView

ISSUE

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

FIX It should be

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


来源:https://stackoverflow.com/questions/38328537/exc-bad-access-when-im-trying-to-delete-a-uitableview-sections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!