Skip to content

Instantly share code, notes, and snippets.

@hyperspacemark
Created August 22, 2013 19:47
Show Gist options
  • Select an option

  • Save hyperspacemark/6311915 to your computer and use it in GitHub Desktop.

Select an option

Save hyperspacemark/6311915 to your computer and use it in GitHub Desktop.
@protocol ATFetchedResultsControllerUpdateDelegate;
@interface ATFetchedResultsControllerDelegate : NSObject <NSFetchedResultsControllerDelegate>
@property (weak, nonatomic, readonly) UITableView *tableView;
@property (weak, nonatomic) id<ATFetchedResultsControllerUpdateDelegate> updateDelegate;
- (id)initWithTableView:(UITableView *)tableView;
@end
@protocol ATFetchedResultsControllerUpdateDelegate <NSObject>
@required
- (void)fetchedResultsControllerDelegate:(ATFetchedResultsControllerDelegate *)delegate wantsToConfigureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
@end
#import "ATFetchedResultsControllerDelegate.h"
@implementation ATFetchedResultsControllerDelegate
- (id)initWithTableView:(UITableView *)tableView
{
self = [super init];
if (!self) {
return nil;
}
_tableView = tableView;
return self;
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
[self.updateDelegate fetchedResultsControllerDelegate:self wantsToConfigureCell:[self.tableView cellForRowAtIndexPath:indexPath] forRowAtIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment