Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eoghain/3055555 to your computer and use it in GitHub Desktop.
Save eoghain/3055555 to your computer and use it in GitHub Desktop.
UITableView+FetchedResultsControllerDelegate
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface UITableView (FetchedResultsControllerDelegate) <NSFetchedResultsControllerDelegate>
@end
@implementation UITableView (FetchedResultsControllerDelegate)
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type)
{
case NSFetchedResultsChangeInsert:
{
[self insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
break;
}
case NSFetchedResultsChangeDelete:
{
[self deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
break;
}
case NSFetchedResultsChangeUpdate:
{
[self reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
}
case NSFetchedResultsChangeMove:
{
[self deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[self insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
break;
}
default:
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationTop];
break;
case NSFetchedResultsChangeDelete:
[self deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationTop];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self endUpdates];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment