Question

I am having trouble deleting an object in an NSMutableSet using core Data. I am trying to delete a "player" object in the second section of my tableview. I am getting the error;

Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out

Solution

Take a look at my code.

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


        if (indexPath.section==0) {

        }else{

            _player = [self.fetchedResultsController.fetchedObjects objectAtIndex: indexPath.row];
            [self.managedObjectContext deleteObject:_player];
            [self performFetch];
            [self.managedObjectContext save:nil];

            // here the solution to make it works...
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1]  withRowAnimation:UITableViewRowAnimationFade];               
        }            
    }   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section){
        case 0:
            return 4;
        case 1:
            return [self.fetchedResultsController.fetchedObjects count];
    }
    return 0;
}
Was it helpful?

Solution

Generally when you need to delete or remove element from a table view, you need to perform a two steps operation:

  1. deal with the model
  2. deal with the table animation

You performed only the first part. To complete you need to perform a call like the following

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
        withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

You need to wrap the deleteRowsAtIndexPaths:withRowAnimation: between beginUpdates and endUpdates if you perform multiple animations for your table, e.g delete, modify, etc. Here it is not the case but you can do it anyway.

When you use core data you could have this for free (you have to write some code) you NSFetchedResultsController with its delegate NSFetchedResultsControllerDelegate. So, when you remove an element with deleteObject call (step 1) the delegate will respond automatically to that change and will perform step 2.

Take a look to at How to use NSFetchedResultsController to have an understanding on how to set up it correctly.

The fix for the code above is to use

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1]  withRowAnimation:UITableViewRowAnimationFade];

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top