Last active
February 1, 2016 06:29
-
-
Save emmasteimann/2f2df581a0423630ccb5 to your computer and use it in GitHub Desktop.
UICollectionView and NSFetchedController Delegate Snippet
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)controllerWillChangeContent:(NSFetchedResultsController *)controller { | |
_sectionChanges = [[NSMutableArray alloc] init]; | |
_itemChanges = [[NSMutableArray alloc] init]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller | |
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo | |
atIndex:(NSUInteger)sectionIndex | |
forChangeType:(NSFetchedResultsChangeType)type { | |
NSMutableDictionary *change = [[NSMutableDictionary alloc] init]; | |
change[@(type)] = @(sectionIndex); | |
[_sectionChanges addObject:change]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller | |
didChangeObject:(id)anObject | |
atIndexPath:(NSIndexPath *)indexPath | |
forChangeType:(NSFetchedResultsChangeType)type | |
newIndexPath:(NSIndexPath *)newIndexPath { | |
NSMutableDictionary *change = [[NSMutableDictionary alloc] init]; | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
change[@(type)] = newIndexPath; | |
break; | |
case NSFetchedResultsChangeDelete: | |
change[@(type)] = indexPath; | |
break; | |
case NSFetchedResultsChangeUpdate: | |
change[@(type)] = indexPath; | |
break; | |
case NSFetchedResultsChangeMove: | |
change[@(type)] = @[indexPath, newIndexPath]; | |
break; | |
} | |
[_itemChanges addObject:change]; | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { | |
[self.collectionView performBatchUpdates:^{ | |
for (NSDictionary *change in _sectionChanges) { | |
[change enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | |
NSFetchedResultsChangeType type = [key unsignedIntegerValue]; | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
[self.collectionView insertSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]]; | |
break; | |
} | |
}]; | |
} | |
for (NSDictionary *change in _itemChanges) { | |
[change enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | |
NSFetchedResultsChangeType type = [key unsignedIntegerValue]; | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
[self.collectionView insertItemsAtIndexPaths:@[obj]]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[self.collectionView deleteItemsAtIndexPaths:@[obj]]; | |
break; | |
case NSFetchedResultsChangeUpdate: | |
[self.collectionView reloadItemsAtIndexPaths:@[obj]]; | |
break; | |
case NSFetchedResultsChangeMove: | |
[self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]]; | |
break; | |
} | |
}]; | |
} | |
} completion:^(BOOL finished) { | |
_sectionChanges = nil; | |
_itemChanges = nil; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Courtesy of: http://jose-ibanez.tumblr.com/post/38494557094/uicollectionviews-and-nsfetchedresultscontrollers