Search code examples
objective-cios4uitableview

Add/Delete UITableViewCell with animation?


I know this might sound like a dumb question, but I have looked every where. How can I do this?

I know how to do this with a swype-to-delete method, but how cam I do it outside that function?

Please post some code samples.

Thanks!
Coulton


Solution

  • [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
    

    insertIndexPaths is an array of NSIndexPaths to be inserted to your table.

    deleteIndexPaths is a array of NSIndexPaths to be deleted from your table.

    Example array format for index paths :

    NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
            [NSIndexPath indexPathForRow:0 inSection:0],
            [NSIndexPath indexPathForRow:1 inSection:0],
            [NSIndexPath indexPathForRow:2 inSection:0],
            nil];