Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdhansen5

macrumors member
Original poster
Nov 22, 2010
43
0
Colorado
I've got a UIButton on the TableViewCell of my app, which I'm trying to use to delete the cell. (I know I should use standard swipe-to-delete, but it doesn't fit with my UX and this will be much better). I've used the following code to try to delete the cell. I get no warnings about any of it in Xcode, but when I run the app and try to delete a cell, I get a SIGTRAP crash and the cell is not deleted, as well as the log says "deleting (null)" opposed to deleting (ItemName). Does anyone have any suggestions on how I could fix this issue? A huge thanks to anyone that can help out. I've been struggling with this same issue for more than a week, and I cannot figure it out at all.

Code:
- (IBAction)deleteButtonTapped:(id)sender {


UIButton *button = (UIButton *)sender;
UIView *backView = (UIView *)button.superview;
UITableViewCell *cell = (UITableViewCell *)backView.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

Item *itemToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Deleting (%@)", itemToDelete);
[self.managedObjectContext deleteObject:itemToDelete];
[self.managedObjectContext save:nil];

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self performFetch];
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
Not sure the last two lines are needed. As fetched results controller is listening to notifications from your context. It will hear that you've deleted the item then inform tableview to delete the row in question. Unless your not having the fetched results controller manage updates for you.

The second last line, if it is required should be enclosed within a tableView update block. Sorry just being preemptive on what will be the next problem, but your code isn't getting that far yet.

As code breaker said check if the right index path is being returned.
Also maybe check the class of the object your telling the compiler is a uitableviewcell maybe there is sneaky view in the chain. If its not a cell then the tableview won't have an index path for it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.