I have a UITableViewController with an Edit button. When this is clicked, the user can delete or add rows to the UITableView. Deleting rows works fine. But when I want to add a new row, I get a short animation of an empty cell coming from the left and when the animation is over, the table view looks exactly as it looked before the method was called. When I add a new cell, I add a new object to the data array that feeds my table view and then add the cell. That data array gets updated. So if I call [tableView reloadData] after I have added the new cell, I get to see the new cell but without any animation. And I really would like to have the animation as well.
I have a working example of the same thing. I realized that after tableView:commitEditingStyle:forRowAtIndexPath: gets called, the table view’s data source method tableView:cellForRowAtIndexPath: gets automatically called. In my case, it doesn’t. I guess that’s the reason why I’m not seeing the new cell. Any ideas why this is happening?
This is my `tableView:commitEditingStyle:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source
[self.tableData removeObjectAtIndex:[indexPath row]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.tableData forKey:self.key];
[defaults synchronize];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *textField = (UITextField *)[[cell contentView] viewWithTag:kTextFieldTag];
NSString *textFieldText = [textField text];
if (textFieldText != nil)
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
NSString *string = [[NSString alloc] initWithString:textFieldText];
[self.tableData addObject:string];
[string release];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.tableData forKey:self.key];
[defaults synchronize];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// If reloadData is being called, the new cell is displayed but without the animation
// [tableView reloadData];
}
else
{
// Display alert view
// Code for displaying an AlertView
}
}
}
Try wrapping your insert call with [tableView beginUpdates] [tableView endUpdates]. From the docs: