iOS 5 - Issue with deleting UITableViewCell with NSFetchedResultsControlller and Cust

Discussion in 'iOS Programming' started by mdhansen5, Jun 14, 2012.

  1. mdhansen5 macrumors member

    Joined:
    Nov 22, 2010
    Location:
    Colorado
    #1
    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];
     
  2. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    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.
     

Share This Page