Inserting and deleting UITableViewCell at the same time not working

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:

Image here:

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:

Image here:

...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.


Struggled to get this to work. Here's my code to add a row to my tableView:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView beginUpdates];
[dataSource insertObject:[artistField text] atIndex:0];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];

Need Your Help

Get done a php file with a simple click with jquery

javascript php jquery html

I am Totally Newbie, i just want a very very simple example for my question.

How to permanently disable Wi-Fi, Bluetooth and USB Mass Storage in Android

android android-camera android-wifi android-bluetooth

I'm working on an Android app which requires to disable WiFi, Bluetooth and Mass Storage options.