Skip to content

Instantly share code, notes, and snippets.

@mlaster
Created May 24, 2012 21:07
Show Gist options
  • Save mlaster/2784243 to your computer and use it in GitHub Desktop.
Save mlaster/2784243 to your computer and use it in GitHub Desktop.
UITableView animation
- (void)animateTableViewUpdate:(UITableView *)inTableView FromOldList:(NSArray *)oldList newList:(NSArray *)newList comparisionKey:(NSString *)inKey {
NSMutableDictionary *oldMap = [NSMutableDictionary dictionary];
NSMutableDictionary *newMap = [NSMutableDictionary dictionary];
NSMutableSet *oldSet = [NSMutableSet set];
NSMutableSet *newSet = [NSMutableSet set];
NSMutableSet *deleteSet = nil;
NSMutableSet *insertSet = nil;
static NSInteger offset = 0;
[oldList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
[oldMap setObject:obj forKey:[obj valueForKey:inKey]];
}];
[newList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
[newMap setObject:obj forKey:[obj valueForKey:inKey]];
}];
[oldSet addObjectsFromArray:[oldMap allKeys]];
[newSet addObjectsFromArray:[newMap allKeys]];
deleteSet = [oldSet mutableCopy];
[deleteSet minusSet:newSet];
insertSet = [newSet mutableCopy];
[insertSet minusSet:oldSet];
[inTableView beginUpdates];
// Process moves
for (NSUInteger i = 0; i < [newList count]; i++) {
NSString *candidateObject = newList[i];
NSString *candidateKey = [candidateObject valueForKey:inKey];
if ([deleteSet containsObject:candidateKey] == NO &&
[insertSet containsObject:candidateKey] == NO) {
NSUInteger oldIndex = [oldList indexOfObject:[oldMap objectForKey:candidateKey]];
NSUInteger newIndex = [newList indexOfObject:[newMap objectForKey:candidateKey]];
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:(NSInteger)oldIndex + offset inSection:0];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:(NSInteger)newIndex + offset inSection:0];
if (oldIndex != newIndex) {
// NSLog(@"MOVE row at %@ to %@", oldIndexPath, newIndexPath);
[inTableView moveRowAtIndexPath:oldIndexPath toIndexPath:newIndexPath];
}
}
}
// Process deletes
for (NSString *deleteKey in deleteSet) {
NSUInteger index = [oldList indexOfObject:[oldMap objectForKey:deleteKey]];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(NSInteger)index + offset inSection:0];
NSParameterAssert(index != NSNotFound);
// NSLog(@"DELETE row at %@", [NSArray arrayWithObject:indexPath]);
[inTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
// Process inserts
for (NSString *insertKey in insertSet) {
NSUInteger index = [newList indexOfObject:[newMap objectForKey:insertKey]];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(NSInteger)index + offset inSection:0];
NSParameterAssert(index != NSNotFound);
// NSLog(@"INSERT row at %@", [NSArray arrayWithObject:indexPath]);
[inTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[inTableView endUpdates];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment