Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generic UITableView/UICollectionView data source for multiple sections
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
typedef void(^ADVCellConfigurationBlock)(id cell, id object);
@interface ADVDataSource : NSObject <UITableViewDataSource, UICollectionViewDataSource>
@property (readonly, nonatomic) NSArray *sections;
- (instancetype) initWithSections:(NSArray *)sections
reusableCellIdentifier:(NSString *)identifier
cellConfigurationBlock:(ADVCellConfigurationBlock)block;
@end
//
// Copyright © 2013 Yuri Kotov
//
#import "ADVDataSource.h"
#import "ADVSectionInfo.h"
@interface ADVDataSource ()
@property (readonly, nonatomic) NSString *cellIdentifier;
@property (readonly, nonatomic) ADVCellConfigurationBlock configurationBlock;
@end
@implementation ADVDataSource
#pragma mark - ADVManagedDataSource
- (instancetype) initWithSections:(NSArray *)sections
reusableCellIdentifier:(NSString *)identifier
cellConfigurationBlock:(ADVCellConfigurationBlock)block
{
if ((self = [super init]))
{
_sections = sections;
_configurationBlock = block;
_cellIdentifier = identifier;
}
return self;
}
- (NSInteger) numberOfSections
{
return self.sections.count;
}
- (NSInteger) numberOfItemsInSection:(NSInteger)section
{
id <ADVSectionInfo> sectionInfo = self.sections[section];
return sectionInfo.numberOfObjects;
}
- (id) view:(id)view cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
id <ADVSectionInfo> sectionInfo = self.sections[indexPath.section];
id object = sectionInfo.objects[indexPath.row];
UITableViewCell *cell = [view dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
self.configurationBlock(cell, object);
return cell;
}
#pragma mark - UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [self numberOfSections];
}
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <ADVSectionInfo> sectionInfo = self.sections[section];
return sectionInfo.name;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self numberOfItemsInSection:section];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self view:tableView cellForItemAtIndexPath:indexPath];
}
#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
{
return [self view:collectionView cellForItemAtIndexPath:indexPath];
}
@end
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
@protocol ADVSectionInfo <NSObject>
@required
@property (readonly, nonatomic) NSArray *objects;
@property (readonly, nonatomic) NSUInteger numberOfObjects;
@optional
@property (readonly, nonatomic) NSString *name;
@property (readonly, nonatomic) NSString *indexTitle;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment