Kyle Rosenbluth
Kyle Rosenbluth

Reputation: 1682

EXC_BAD_ACCESS When Trying To Delete TableView Section

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.

Thanks,

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

Upvotes: 0

Views: 496

Answers (1)

jmstone617
jmstone617

Reputation: 5707

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.

Upvotes: 1

Related Questions