Last active
March 24, 2021 15:03
-
-
Save ivanzoid/8353510 to your computer and use it in GitHub Desktop.
Insert & delete rows in UITableView with animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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