views:

38

answers:

2

Is it not possible to remove several rows from UITableView while adding no new rows? I thought there was support for batch insertion and deletion but it appears that you can only adjust the table size by 1 item from the previous count.

When I attempt to remove a whole bunch of rows without adding anything new, I get the error:

Invalid update: invalid number of rows in section 2. 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 (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'

This is how I'm doing the deletes:

// The section initially has 7 rows. I want to remove 5 of them.
// The underlying table store data has been cleared of the 5 rows
// content as of this point

int newRowCount = 5;
int aSection = 2;

// Create the index paths of the rows I want to delete

NSMutableArray *deletePaths = [NSMutableArray arrayWithCapacity:newRowCount];
for (int ix = 0; ix < newRowCount; ++ix)
{
    [deletePaths addObject:[NSIndexPath indexPathForRow:ix inSection:aSection]];                
}

// Start the updates
[aTableview beginUpdates];

if (deletePaths && [deletePaths count] > 0)
{
    [aTableview deleteRowsAtIndexPaths:deletePaths 
                      withRowAnimation:UITableViewRowAnimationNone];
}

[aTableview endUpdates];

Apple's UITableView Documentation I'm following: link text

A: 

A bit of a guess since I can't see this part of the code, but make sure that

- (NSInteger)tableView:(UITableView *)tableView
         numberOfRowsInSection:(NSInteger)section

is returning the right number. It sounds like you're deleting rows from a section without this method returning the new number of rows.

Nimrod
A: 

I just dealt with this exact same error an hour ago. In addition to deleting the rows themselves, you need to remove the deleted objects from your data array (assuming that you are using a data array to store the list of stuff that is displayed in the table view). Be sure that you remove the objects from your data array before you call deleteRowsAtIndexPaths. Calling that method causes numberOfRowsInSection to get called; and as was already mentioned, you need to make sure that that method is not returning the same number of rows that it was before the delete statement.

GendoIkari