I’m having quite a bit of pain inserting and deleting UITableViewCells from the same UITableView!
I don’t normally post code, but I thought this was the best way of showing where I’m having the problem:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 5; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (iSelectedSection == section) return 5; return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //NSLog(@'drawing row:%d section:%d', [indexPath row], [indexPath section]); static NSString *CellIdentifier = @'Cell'; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } if (iSelectedSection == [indexPath section]) { cell.textColor = [UIColor redColor]; } else { cell.textColor = [UIColor blackColor]; } cell.text = [NSString stringWithFormat:@'Section: %d Row: %d', [indexPath section], [indexPath row]]; // Set up the cell return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic -- create and push a new view controller if ([indexPath row] == 0) { NSMutableArray *rowsToRemove = [NSMutableArray array]; NSMutableArray *rowsToAdd = [NSMutableArray array]; for(int i=0; i<5; i++) { //NSLog(@'Adding row:%d section:%d ', i, [indexPath section]); //NSLog(@'Removing row:%d section:%d ', i, iSelectedSection); [rowsToAdd addObject:[NSIndexPath indexPathForRow:i inSection:[indexPath section]]]; [rowsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:iSelectedSection]]; } iSelectedSection = [indexPath section]; [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:rowsToRemove withRowAnimation:YES]; [tableView insertRowsAtIndexPaths:rowsToAdd withRowAnimation:YES]; [tableView endUpdates]; } }
This code creates 5 sections, the 1st (indexed from 0) with 5 rows. When you select a section – it removes the rows from the section you had previously selected and adds rows to the section you just selected.
Pictorally, when I load up the app, I have something like this:
http://www.freeimagehosting.net/uploads/1b9f2d57e7.png http://www.freeimagehosting.net/uploads/1b9f2d57e7.png
Image here: http://www.freeimagehosting.net/uploads/1b9f2d57e7.png
After selecting a table row 0 of section 2, I then delete the rows of section 1 (which is selected by default) and add the rows of section 2. But I get this:
http://www.freeimagehosting.net/uploads/6d5d904e84.png http://www.freeimagehosting.net/uploads/6d5d904e84.png
Image here: http://www.freeimagehosting.net/uploads/6d5d904e84.png
…which isn’t what I expect to happen! It seems like the first row of section 2 somehow remains – even though it definitly gets deleted.
If I just do a [tableView reloadData], everything appears as normal… but I obviously forefit the nice animations.
I’d really appreciate it if someone could shine some light here! It’s driving me a little crazy!
Thanks again, Nick.
FYI: This bug seems to have been fixed completely with the 2.2 iPhone update. Thanks Apple! Nick.