Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Can't delete a cell from table view

http://stackoverflow.com/questions/27583972/cant-delete-a-cell-from-table-view

This is a link to my question on stack overflow, I used some of the code there from the core data tutorial here.

I find in the debugger now that the problem is 100% in numberOfRowsInSection method...

If someone can please take a look and guid me that will be awesome,

thanks

4 Answers

you might need to actually delete the row from the tableView:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    Target *target = [localDataManager.fetchedResultController objectAtIndexPath:indexPath];

    [[localDataManager.stack managedObjectContext] deleteObject:target];

    [localDataManager.stack saveContext];


    if ([_delegate respondsToSelector:@selector(didDeleteObject)]) {
        [_delegate didDeleteObject];
    }

    // delete row in tableView
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

it's not it bro, i'v tried it, theres also someone in stack overflow that suggested that. the problem is in the numberOfRowsInSection method apparently..

Thanks!

what does your delegate method didDeleteObject do? that could be causing an issue possibly. try removing that line and see if you still get the same result:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    Target *target = [localDataManager.fetchedResultController objectAtIndexPath:indexPath];

    [[localDataManager.stack managedObjectContext] deleteObject:target];

    [localDataManager.stack saveContext];

}

i have tried to delete the delegate method, nothing different. this is the didDeleteObject methane in my HomeViewController:

-(void)didDeleteObject{
    [localDataManager reloadData];
    [self updateCurrentObjectToDisplay];
}

-(void)updateCurrentObjectToDisplay {
    Target *currentObject = [[localDataManager.fetchedResultController fetchedObjects] firstObject];
    self.homeLabel.text = currentObject.body;
}

Now something weird is happening, I don't see the deletion animation, but when I terminate the app and opening it again the changes were made..

alright, did removing the line from commitEditingStyle still give you teh same error?

just updated the answer

alright, well unfortunately I dont have much experience with core data so I cannot be of much help, hopefully someone on SO can help you out.

ok, thanks for trying man!

no problem! honestly it seems like

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    Target *target = [localDataManager.fetchedResultController objectAtIndexPath:indexPath];

    [[localDataManager.stack managedObjectContext] deleteObject:target];

    [localDataManager.stack saveContext];

}

would work fine. not sure why its not.

I was hoping it would have been an issue with the didDeleteObject method since it would have been easier to track down the problem. But if you get the same error without calling that method the problem is definitely somewhere else.

something is indirectly affecting the return value of your numberOfRows method, but not too sure what it is/how to find it.

good luck