UITableViewRowAnimationBottom doesn't work for last row

Posted by GendoIkari on Stack Overflow See other posts from Stack Overflow or by GendoIkari
Published on 2010-12-22T19:29:51Z Indexed on 2011/01/04 20:53 UTC
Read the original article Hit count: 182

I've come across a very similar question here: Inserting row to end of table with UITableViewRowAnimationBottom doesn't animate., though no answers have been given. His code was also a little different than mine.

I have an extremely simple example, built from the Navigation application template.

NSMutableArray *items;

- (void)viewDidLoad {
    [super viewDidLoad];
    items = [[NSMutableArray array] retain];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem)] autorelease];
}

- (void)addItem{
    [items insertObject:@"new" atIndex:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [items objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [items removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }   
}

The problem is, when I either insert or delete the very last row in the table, the animation doesn't work at all; the row just appears or disappears instantly. This only happens with UITableViewRowAnimationBottom, but that's the animation that makes the most sense for creating or deleting table cells in this way. Is this a bug in Apple's framework? Or does it do this on purpose? Would it make sense to add an extra cell to the count, and then setup this cell so that it looks like it's not there at all, just to get around this behavior?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c