Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Invalid update: invalid number of rows in section 0

I've read all of the related posts regarding this and I am still having 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 delete and remove the object from the 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 by having my number of sections relying on the count of the array I'm editing it would ensure the proper number of rows? Can't this be done without having to reload the table when you delete a row anyway?

like image 946
user3085646 Avatar asked Feb 19 '14 03:02

user3085646


1 Answers

You need to remove the object from your 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 little by using the array creation shortcut @[]:

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
like image 178
Undo Avatar answered Sep 25 '22 15:09

Undo