Skip to content

Instantly share code, notes, and snippets.

@gugell
Forked from advantis/ADVDataSource.h
Created December 4, 2018 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gugell/1bec06453d3375debc6b1ea72b9c79e9 to your computer and use it in GitHub Desktop.
Save gugell/1bec06453d3375debc6b1ea72b9c79e9 to your computer and use it in GitHub Desktop.
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