3

Welcome! I just spent the entire morning working on this, but no banana; maybe you have an insight!

I am working with a standard NSMutableArray:

self.itemTable = [[NSMutableArray alloc]
                 initWithObjects:@"House.",
                 @"Car.",
                 @"Keys.", nil];

Now when I press 'EDIT' in the navigation bar, and delete a row, I get following Error Message

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), 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).'

I use this code for the 'delete' command:

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


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

}

Any insights on how to make the delete row function work?

EDIT:

Found it: Placing the following line of code in the worked:

[self.itemTable removeObjectAtIndex:indexPath.row];

Which makes the delete code:

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

[self.itemTable removeObjectAtIndex:indexPath.row];

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

}
etolstoy
  • 1,798
  • 21
  • 33
MikeDaMike
  • 63
  • 1
  • 5
  • See http://stackoverflow.com/questions/1016200/how-can-i-make-deleterowsatindexpaths-work-with-generictableviewcontroller – J_D Apr 17 '12 at 20:06
  • 1
    Try to add [tableView reloadData] in commitEditingStyle method below the [tableVIew deleteRowsAtIndexPaths...] It crashes probably because you are modifying your data source (array with data) and you don't update it.. But then again, I can be wrong, because I don't have much experience :) – Yanchi Apr 08 '13 at 15:23

3 Answers3

1

I faced the same problem. After working around found a solution. Here my crash was actually due to calling tableview reload after removing item from array. in crash report it mentioned that before and after editing/updating tableview rows in section must be equal.

Bellow code is working fine:

 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (editingStyle == UITableViewCellEditingStyleDelete)
     {
        //add code here for when you hit delete
        [self deleteItemAtRowIndex:indexPath];
    }
}


-(void)deleteItemAtRowIndex:(NSIndexPath *)indexpath
{
[pTableDataDataArray removeObjectAtIndex:indexpath.row];

[pTableView beginUpdates];
//[self LoadTempletesData];    no need to reload, simply remove row that to is to be delete
[pTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationNone];
//make changes
[pTableView endUpdates];
}

Hope it helps others

Usman Nisar
  • 3,031
  • 33
  • 41
0

(As you seem to have noticed) you have to update the datasource before you insert or delete rows. Occasionally you will want to do something fancy like add and delete rows. (I've done this to hide a subsection of rows and show another section). If you need to do both, you can use this construct

[tableView beginUpdates];
//make changes
[tableView endUpdates];
Brian Broom
  • 497
  • 4
  • 11
0

Remember to update the data source every time you add or delete the content from the table, which is fetched from the data source itself.

Javvadi Rajesh
  • 281
  • 2
  • 10