Created
February 20, 2015 11:48
-
-
Save blork/091ba31738005ad327d8 to your computer and use it in GitHub Desktop.
Reload a tableview with nice animations.
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
- (void)reloadWithUpdatedObjects:(NSArray *)updatedObjects animated:(BOOL)animated | |
{ | |
NSMutableArray *delete = [NSMutableArray array]; | |
NSMutableArray *insert = [NSMutableArray array]; | |
NSMutableArray *changed = [NSMutableArray array]; | |
NSMutableDictionary *moved = [NSMutableDictionary dictionary]; | |
NSInteger deleteIndex = 0; | |
for (NSObject *existingObject in self.objects) | |
{ | |
/** | |
* If new object does not appear in existing results, it has been deleted. | |
*/ | |
if (![updatedObjects containsObject:existingObject]) | |
{ | |
[delete addObject:[NSIndexPath indexPathForItem:deleteIndex inSection:0]]; | |
} | |
deleteIndex++; | |
} | |
NSInteger newIndex = 0; | |
for (NSObject *updatedObject in updatedObjects) | |
{ | |
/** | |
* Find where the objects used to appear. | |
*/ | |
NSInteger oldIndex = [self.commits indexOfObject:updatedObject]; | |
NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:newIndex inSection:0]; | |
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForItem:oldIndex inSection:0]; | |
if (oldIndex == newIndex) | |
{ | |
RLMObject *oldObject = self.commits[oldIndex]; | |
//Not moved, but maybe changed | |
if ([updatedObject isEqual:oldObject]) | |
{ | |
//Nothing changed!! | |
} | |
else | |
{ | |
[changed addObject:newIndexPath]; | |
} | |
} | |
else if (oldIndex != NSNotFound) | |
{ | |
//Moved | |
[moved setObject:newIndexPath forKey:oldIndexPath]; | |
} | |
else if (oldIndex == NSNotFound) | |
{ | |
// New insert | |
[insert addObject:newIndexPath]; | |
} | |
newIndex++; | |
} | |
/** | |
* Replace the old objects with the updated ones. | |
*/ | |
self.objects = updatedObjects; | |
if (insert.count > 0 || delete.count > 0) | |
{ | |
[UIView setAnimationsEnabled:animated]; | |
[self.tableView beginUpdates]; | |
[self.tableView insertRowsAtIndexPaths:insert | |
withRowAnimation:(animated ? UITableViewRowAnimationAutomatic : UITableViewRowAnimationNone)]; | |
[self.tableView deleteRowsAtIndexPaths:delete | |
withRowAnimation:(animated ? UITableViewRowAnimationAutomatic : UITableViewRowAnimationNone)]; | |
for (NSIndexPath *old in [moved allKeys]) | |
{ | |
NSIndexPath *new = [moved objectForKey : old]; | |
[self.tableView moveRowAtIndexPath:old toIndexPath:new]; | |
} | |
[self.tableView endUpdates]; | |
[UIView setAnimationsEnabled:YES]; | |
} | |
for (NSIndexPath *indexPath in changed) | |
{ | |
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; | |
// A method to update the info in your cells | |
[self configureCell:cell atIndexPath:indexPath]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You want the nice animations that you normally get with a fetchedResultsController but you just have dumb objects. Here’s a way to determine what was inserted/updated/deleted.