Last active
February 19, 2016 06:51
-
-
Save kristopherjohnson/7865808 to your computer and use it in GitHub Desktop.
Boilerplate NSFetchedResultsControllerDelegate methods for a UITableViewController subclass
This file contains 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
#pragma mark - NSFetchedResultsControllerDelegate methods | |
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { | |
[self.tableView beginUpdates]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller | |
didChangeObject:(id)anObject | |
atIndexPath:(NSIndexPath *)indexPath | |
forChangeType:(NSFetchedResultsChangeType)type | |
newIndexPath:(NSIndexPath *)newIndexPath { | |
UITableView *tableView = self.tableView; | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
case NSFetchedResultsChangeUpdate: { | |
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; | |
if (cell != nil) { | |
[self configureCell:cell withObject:anObject]; | |
}} | |
break; | |
case NSFetchedResultsChangeMove: | |
[tableView deleteRowsAtIndexPaths:[NSArray | |
arrayWithObject:indexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
[tableView insertRowsAtIndexPaths:[NSArray | |
arrayWithObject:newIndexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
} | |
} | |
- (void)controller:(NSFetchedResultsController *)controller | |
didChangeSection:(id )sectionInfo | |
atIndex:(NSUInteger)sectionIndex | |
forChangeType:(NSFetchedResultsChangeType)type { | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
} | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { | |
[self.tableView endUpdates]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BTW, this snippet of boilerplate from the NSFetchedResultsControllerDelegate documentation is buggy:
See these links for explanation and fixes: