Last active
August 29, 2015 14:14
-
-
Save benjaminsnorris/2e4590f34561a4eb7452 to your computer and use it in GitHub Desktop.
TableView FetchedResultsController
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 <Foundation/Foundation.h> | |
#import <CoreData/CoreData.h> | |
@interface DXListTableViewDataSource : NSObject <UITableViewDataSource> | |
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController; | |
- (void)registerTableView:(UITableView *)tableView; | |
- (void)configureFetchedResultsControllerForTableView:(UITableView *)tableView; | |
@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 "DXListTableViewDataSource.h" | |
#import "EntryController.h" | |
#import "Stack.h" | |
@interface DXListTableViewDataSource() <NSFetchedResultsControllerDelegate> | |
@property (nonatomic, strong) UITableView* tableView; | |
@end | |
@implementation DXListTableViewDataSource | |
- (void)registerTableView:(UITableView *)tableView { | |
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; | |
self.tableView = tableView; | |
} | |
- (void)configureFetchedResultsControllerForTableView:(UITableView *)tableView { | |
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Entry"]; | |
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:NO]]; | |
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[Stack sharedInstance].managedObjectContext sectionNameKeyPath:nil cacheName:nil]; | |
self.fetchedResultsController.delegate = self; | |
[self.fetchedResultsController performFetch:nil]; | |
self.tableView = tableView; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return self.fetchedResultsController.fetchedObjects.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])]; | |
Entry *entry = self.fetchedResultsController.fetchedObjects[indexPath.row]; | |
cell.textLabel.text = entry.title; | |
return cell; | |
} | |
#pragma mark - Fetched Results Controller Delegate | |
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { | |
[self.tableView beginUpdates]; | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { | |
[self.tableView endUpdates]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { | |
switch (type) { | |
case NSFetchedResultsChangeInsert: | |
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeMove: | |
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; | |
break; | |
case NSFetchedResultsChangeUpdate: | |
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
default: | |
break; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment