Last active
January 2, 2016 05:29
-
-
Save kristopherjohnson/8257082 to your computer and use it in GitHub Desktop.
General-purpose implementation of NSFetchedResultsControllerDelegate
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
#import <UIKit/UIKit.h> | |
#import <CoreData/CoreData.h> | |
// Protocol adopted by an object that can configure a table view cell with data from an object | |
@protocol KDJTableViewCellConfigurationDelegate <NSObject> | |
- (void)configureCell:(UITableViewCell *)cell withObject:(NSObject *)object; | |
@end | |
// General-purpose implementation of NSFetchedResultsControllerDelegate | |
@interface KDJFetchedResultsControllerContentUpdater : NSObject <NSFetchedResultsControllerDelegate> | |
// Table view to be updated | |
@property (nonatomic, weak) UITableView *tableView; | |
// Delegate that will configure a cell with updated data when a managed object changes | |
@property (nonatomic, weak) id<KDJTableViewCellConfigurationDelegate> cellConfigurationDelegate; | |
@end |
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
#import "KDJFetchedResultsControllerContentUpdater.h" | |
@implementation KDJFetchedResultsControllerContentUpdater | |
- (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:UITableViewRowAnimationFade]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
} | |
} | |
- (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:@[newIndexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[tableView deleteRowsAtIndexPaths:@[indexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
case NSFetchedResultsChangeUpdate: { | |
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; | |
if (cell != nil) { | |
[self.cellConfigurationDelegate configureCell:cell | |
withObject:anObject]; | |
}} | |
break; | |
case NSFetchedResultsChangeMove: | |
[tableView deleteRowsAtIndexPaths:@[indexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
[tableView insertRowsAtIndexPaths:@[newIndexPath] | |
withRowAnimation:UITableViewRowAnimationFade]; | |
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
BTW, this snippet of boilerplate from the NSFetchedResultsControllerDelegate documentation is buggy:
See these links for explanation and fixes: