Created
October 4, 2014 04:55
-
-
Save benjaminsnorris/5f51ae547453450f975b to your computer and use it in GitHub Desktop.
FetchedResultsControllerDataSource
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
// | |
// | |
// FetchedResultsControllerDataSource.h | |
// | |
// Created by Ben Norris on 10/3/14. | |
// Copyright (c) 2014 BSN Design. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@class NSFetchedResultsController; | |
@protocol FetchedResultsControllerDataSourceDelegate <NSObject> | |
-(void)configureCell:(id)cell withObject:(id)object; | |
-(void)deleteObject:(id)object; | |
@end | |
@interface FetchedResultsControllerDataSource : NSObject <UITableViewDataSource,NSFetchedResultsControllerDelegate> | |
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController; | |
@property (nonatomic, weak) id<FetchedResultsControllerDataSourceDelegate> delegate; | |
@property (nonatomic, copy) NSString *reuseIdentifier; | |
- (id)initWithTableView:(UITableView *)tableView; | |
- (void)registerTableView:(UITableView *)tableView withCell:(id)cell; | |
- (id)selectedItem; | |
@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
// | |
// FetchedResultsControllerDataSource.m | |
// Scores | |
// | |
// Created by Ben Norris on 10/3/14. | |
// Copyright (c) 2014 BSN Design. All rights reserved. | |
// | |
#import "FetchedResultsControllerDataSource.h" | |
@interface FetchedResultsControllerDataSource() | |
@property (nonatomic, strong) UITableView *tableView; | |
@end | |
@implementation FetchedResultsControllerDataSource | |
- (id)initWithTableView:(UITableView *)tableView { | |
self = [super init]; | |
if (self) { | |
self.tableView = tableView; | |
self.tableView.dataSource = self; | |
} | |
return self; | |
} | |
- (void)registerTableView:(UITableView *)tableView withCell:(id)cell { | |
[tableView registerClass:[cell class] forCellReuseIdentifier:self.reuseIdentifier]; | |
} | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | |
return self.fetchedResultsController.sections.count; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return [[self.fetchedResultsController.sections objectAtIndex:section] numberOfObjects]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
id object = [self.fetchedResultsController objectAtIndexPath:indexPath]; | |
id cell = [tableView dequeueReusableCellWithIdentifier:self.reuseIdentifier forIndexPath:indexPath]; | |
[self.delegate configureCell:cell withObject:object]; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { | |
if (editingStyle == UITableViewCellEditingStyleDelete) { | |
[self.delegate deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; | |
} | |
} | |
#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 moveRowAtIndexPath:indexPath toIndexPath:newIndexPath]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
default: | |
break; | |
} | |
} | |
- (void)setFetchedResultsController:(NSFetchedResultsController *)fetchedResultsController { | |
_fetchedResultsController = fetchedResultsController; | |
fetchedResultsController.delegate = self; | |
[fetchedResultsController performFetch:nil]; | |
} | |
- (id)selectedItem { | |
NSIndexPath *path = self.tableView.indexPathForSelectedRow; | |
return path ? [self.fetchedResultsController objectAtIndexPath:path] : nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment