Last active
December 19, 2015 02:28
-
-
Save advantis/5883097 to your computer and use it in GitHub Desktop.
Generic UITableView/UICollectionView data source for Core Data
This file contains 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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import <Foundation/Foundation.h> | |
@protocol ADVKeyedSubscripting <NSObject> | |
- (id) objectForKeyedSubscript:(id)key; | |
@end |
This file contains 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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import <Foundation/Foundation.h> | |
#import "ADVKeyedSubscripting.h" | |
typedef void(^ADVCellConfigurationBlock)(id cell, id object); | |
@interface ADVManagedDataSource : NSObject <ADVKeyedSubscripting, UITableViewDataSource, UICollectionViewDataSource> | |
@property (readonly, nonatomic) NSFetchedResultsController *dataController; | |
- (instancetype) initWithDataController:(NSFetchedResultsController *)controller | |
reusableCellIdentifier:(NSString *)identifier | |
cellConfigurationBlock:(ADVCellConfigurationBlock)block; | |
@end |
This file contains 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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import <CoreData/CoreData.h> | |
#import "ADVManagedDataSource.h" | |
@interface ADVManagedDataSource () | |
@property (readonly, nonatomic) NSString *cellIdentifier; | |
@property (readonly, nonatomic) ADVCellConfigurationBlock configurationBlock; | |
@end | |
@implementation ADVManagedDataSource | |
#pragma mark - ADVManagedDataSource | |
- (instancetype) initWithDataController:(NSFetchedResultsController *)controller | |
reusableCellIdentifier:(NSString *)identifier | |
cellConfigurationBlock:(ADVCellConfigurationBlock)block | |
{ | |
if ((self = [super init])) | |
{ | |
_configurationBlock = block; | |
_cellIdentifier = identifier; | |
_dataController = controller; | |
} | |
return self; | |
} | |
- (NSInteger) numberOfSections | |
{ | |
return [self.dataController.sections count]; | |
} | |
- (NSInteger) numberOfItemsInSection:(NSInteger)section | |
{ | |
return [self.dataController.sections[section] numberOfObjects]; | |
} | |
#pragma mark - ADVKeyedSubscripting | |
- (id) objectForKeyedSubscript:(NSIndexPath *)indexPath | |
{ | |
NSParameterAssert([indexPath isKindOfClass:[NSIndexPath class]]); | |
return [self.dataController objectAtIndexPath:indexPath]; | |
} | |
#pragma mark - UITableViewDataSource | |
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return [self numberOfSections]; | |
} | |
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section | |
{ | |
return [self.dataController.sections[section] name]; | |
} | |
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return [self numberOfItemsInSection:section]; | |
} | |
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath]; | |
self.configurationBlock(cell, self[indexPath]); | |
return cell; | |
} | |
#pragma mark - UICollectionViewDataSource | |
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView | |
{ | |
return self.numberOfSections; | |
} | |
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section | |
{ | |
return [self numberOfItemsInSection:section]; | |
} | |
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView | |
cellForItemAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier forIndexPath:indexPath]; | |
self.configurationBlock(cell, self[indexPath]); | |
return cell; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment