Skip to content

Instantly share code, notes, and snippets.

@ivanzoid
Last active March 24, 2021 15:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ivanzoid/8353510 to your computer and use it in GitHub Desktop.
Save ivanzoid/8353510 to your computer and use it in GitHub Desktop.
Insert & delete rows in UITableView with animation
NSMutableArray *indexPathsToDelete = [NSMutableArray new];
for (Object *object in newObjects)
{
if (![currentObjects containsObject:object]) {
int row = [newObjects indexOfObject:object];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[indexPathsToDelete addObject:indexPath];
}
}
NSMutableArray *indexPathsToAdd = [NSMutableArray new];
for (Object *object in currentObjects)
{
if (![newObjects containsObject:object]) {
int row = [currentObjects indexOfObject:object];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[indexPathsToAdd addObject:indexPath];
}
}
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment