EXC_BAD_ACCESS When Trying To Delete TableView Section

Go To StackoverFlow.com


I have a tableview with several sections. At a certain point, I want to delete a section from the tableView. To do this, I have a long-tap gesture on the headers, and on the long tap I bring up a UIMenuController like so:

UIMenuController *deleteMenu = [UIMenuController sharedMenuController];
UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete"     action:@selector(deleteCell:)];
[deleteMenu setMenuItems:[NSArray arrayWithObject:delete]];
[deleteMenu update];
[deleteMenu setTargetRect:CGRectMake(0, 0, 320, 460) inView:self.superview];
[deleteMenu setMenuVisible:YES animated:YES];

This is done in my custom view subclass for the headers. in the deleteCell: method, I call a delegate method (the delegate being the view controller that owns the tableview). In the implementation of the delegate method, I try to delete a section like so (section being an int):

[statsTable deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];

At this line, I get an EXC_BAD_ACCESS. The weird thing is, Xcode also crashes right when this happens, so I am not able to see the cause for the BAD_ACCESS. If anybody knows why this is happening, your help would be greatly appreciated.


EDIT:: Found the solution, the vc that has the tableview has to become the first responder in order for it to allow you to delete something from the tableview. Thanks

2012-04-03 20:54
by Kyle Rosenbluth
Have you tried running with NSZombies enabled - CodaFi 2012-04-03 20:55
No, can you tell me how to do that? I thought I wouldn't need to because I am using ARC - Kyle Rosenbluth 2012-04-03 20:57
Hm, with ARC you're right, you shouldn't have to. do you call -beginUpdates and -endUpdates before you delete the section - CodaFi 2012-04-03 20:59
I just tried adding begin and end updates around the deletion, it did not help. I feel like it may have something to do with the menu controller as the deletion worked in an older version that didn't have it. Could it be because the headerView is set to firstResponder at the time - Kyle Rosenbluth 2012-04-03 21:06


UIMenuController has a delete MenuItem, which sends the delete: message. I would suggest using that instead of copying that with your own delete.

As an aside, you can very easily enabled NSZombies in Xcode 4.3 by clicking on Manage Schemes (you can access this by clicking the bar that has your current scheme/device, like in the screen shot below...) enter image description here

then, in the screen that pops up, click edit, and you should see this...

enter image description here

Check "Enable Zombie Objects" and you're good to go.

2012-04-03 21:06
by jmstone617
Thanks, I tried wrapping them in a begin/end updates and I still get the same error. Your tutorial on NSZombies is very useful, thanks for that - Kyle Rosenbluth 2012-04-03 21:12