Generic UITableView/UICollectionView data source for Core Data
//
// 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
//
// 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
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
@protocol ADVKeyedSubscripting <NSObject>
- (id) objectForKeyedSubscript:(id)key;
@end