0

I can delete items from the tableView if all of the notes are in one section. As soon as i add items to multiple sections, deleting the note from another section crashes the app with the error:

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

Does anyone know the cause or solution for this?

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

if (editingStyle == UITableViewCellEditingStyleDelete) {

    // Delete the row from the data source
    [self.tableView beginUpdates];
    [self.notes removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight ];

    [self.data writeToFile:self.path atomically:YES];
    [self.tableView endUpdates];

}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3001526
  • 81
  • 2
  • 12
  • Is this question connected to the one about section in cellForRowAtIndexPath: I had a look before? Do you use - if section and NSPredictate in that method? – Greg Dec 09 '13 at 19:39
  • Yes they're connected. I got the cellForIndexPath working with NSPreedicate and it adds the item to the right section but i do not use NSPredicate in deleting rows – user3001526 Dec 09 '13 at 19:48
  • It would be much easier if you rearrange your data source array to reflect the section/rows structure, as for example suggested here http://stackoverflow.com/questions/20454366/number-of-rows-in-section#comment30562892_20454366 or here http://stackoverflow.com/a/20217199/1187415. – Martin R Dec 09 '13 at 20:02

1 Answers1

0

If you use predicate in cellForRowAtIndexPath: you should use it when you delete your data from array because the indexPath.row doesn't match your array index. Try something like that:

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [self.tableView beginUpdates];
        if (indexPath.section == 0){
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"Category =[cd] %@", @"Category 1"];
            NSArray * catArray = [self.notes filteredArrayUsingPredicate:pred];
            [self.notes removeObject:catArray[indexPath.row]]
             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight ];


        } //else other section ....

        [self.data writeToFile:self.path atomically:YES];
        [self.tableView endUpdates];
    }
Greg
  • 25,317
  • 6
  • 53
  • 62
  • Note that `[self.notes removeObject:catArray[indexPath.row]]` might remove a wrong element if the array contains duplicate elements. – Martin R Dec 09 '13 at 19:57
  • Thanks, but this gives reason: 'Invalid update: invalid number of rows in section 0... – user3001526 Dec 09 '13 at 20:11
  • And thanks Martin R, thats not a problem for me at the moment – user3001526 Dec 09 '13 at 20:11
  • ahh, wait - did you mean if (indexPath.row == 0)... ? – user3001526 Dec 09 '13 at 20:16
  • No it's need to be indexPath.section.try move beginUpdates and endUpdates in the if statement. – Greg Dec 09 '13 at 20:21
  • i tried that same issue, it works with indexPath.row though? which makes more sense to me. If its the only row left in the section(ie indexPath.row =0), then it needs to remove the item from the catArray because in numberOfRowsInSection it returns the catArray count – user3001526 Dec 09 '13 at 20:30