'Invalid update: invalid row count in section 0

I read all the posts related to this and I still have an error:

'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), 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).' 

Here are the details:

in .h I have an NSMutableArray :

 @property (strong,nonatomic) NSMutableArray *currentCart; 

In .m my numberOfRowsInSection looks like this:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return ([currentCart count]); } 

To enable deletion and deletion of an object from an array:

 // Editing of rows is enabled - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //when delete is tapped [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [currentCart removeObjectAtIndex:indexPath.row]; } } 

I thought that if the number of my partitions depends on the amount of array I am editing, will it provide the correct number of rows? Can you do this without reloading the table when you delete the row anyway?

+62
arrays ios objective-c nsmutablearray
Feb 19 '14 at 3:54
source share
1 answer

You need to remove the object from the data array before you call deleteRowsAtIndexPaths:withRowAnimation: So your code should look like this:

 // Editing of rows is enabled - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //when delete is tapped [currentCart removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } 

You can also simplify your code a bit by using the @[] array creation shortcut:

 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
+147
Feb 19 '14 at 3:59
source share



All Articles