0

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;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Luke
  • 612
  • 1
  • 6
  • 19

1 Answers1

1

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Hi my NSFetchedResults controller is set up basically identical to that. Because I used that example when building this app. I am not really sure by your answer however what part of my code is going wrong – Luke Jul 08 '12 at 14:48
  • I changed my code above, the app will delete the object but it crashes when reloading the tableview... – Luke Jul 08 '12 at 15:01
  • Have you tried to put the code I provided instead of reloading table view data? Does the crash is the same as before? – Lorenzo B Jul 08 '12 at 15:03
  • Are using also the NSFetchedResultsControllerDelegate? If yes, remove it or do the contrary. Leave the NSFetchedResultsControllerDelegate and comment the other. – Lorenzo B Jul 08 '12 at 15:41
  • Yes I am using a delegate. That is how I am getting my fetch request... So I am not sure what you mean. Delete the begin and end updates code? because If I do that the code still crashes. – Luke Jul 08 '12 at 15:46
  • If for delegate you mean that you have implemented the methods of NSFetchedResultsControllerDelegate, yes, remove it. Maybe you could post some other code and I try to help you. Here it's difficult to understand what is going on. Does your implementation look like the [following](https://github.com/acburk/CoreData-Tutorial-1/blob/Tutorial3/FailedBankCD/FBCDMasterViewController.m)? – Lorenzo B Jul 08 '12 at 15:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13582/discussion-between-luke-and-flex-addicted) – Luke Jul 08 '12 at 15:54