Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting and deleting UITableViewCell at the same time not working
    text
    copied!<p>I'm having quite a bit of pain inserting and deleting UITableViewCells from the same UITableView!</p> <p>I don't normally post code, but I thought this was the best way of showing where I'm having the problem:</p> <hr> <pre><code>- (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&lt;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]; } } </code></pre> <hr> <p>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.</p> <p>Pictorally, when I load up the app, I have something like this:</p> <p><a href="http://www.freeimagehosting.net/uploads/1b9f2d57e7.png" rel="noreferrer">http://www.freeimagehosting.net/uploads/1b9f2d57e7.png http://www.freeimagehosting.net/uploads/1b9f2d57e7.png</a></p> <p>Image here: <a href="http://www.freeimagehosting.net/uploads/1b9f2d57e7.png" rel="noreferrer">http://www.freeimagehosting.net/uploads/1b9f2d57e7.png</a></p> <p>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:</p> <p><a href="http://www.freeimagehosting.net/uploads/6d5d904e84.png" rel="noreferrer">http://www.freeimagehosting.net/uploads/6d5d904e84.png http://www.freeimagehosting.net/uploads/6d5d904e84.png</a></p> <p>Image here: <a href="http://www.freeimagehosting.net/uploads/6d5d904e84.png" rel="noreferrer">http://www.freeimagehosting.net/uploads/6d5d904e84.png</a></p> <p>...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.</p> <p>If I just do a [tableView reloadData], everything appears as normal... but I obviously forefit the nice animations.</p> <p>I'd really appreciate it if someone could shine some light here! It's driving me a little crazy!</p> <p>Thanks again, Nick.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload