Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for data sources with heterogeneous cell reuse identifiers #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion TGRDataSource/TGRArrayDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
Initializes the data source.

@param items The items managed by the data source.
@param reuseIdentifier The cell reuse identifier.
@param reuseIdentifier The default cell reuse identifier.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested...

@param configureCellBlock A block that will be called when the view asks for a cell in a particular location.

@return An initialized data source.
Expand All @@ -45,5 +45,19 @@
cellReuseIdentifier:(NSString *)reuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock;

/**
Initializes the data source with a custom cell reuse identifier block.

@param items The items managed by the data source.
@param reuseIdentifierBlock A block that will be called when the view asks for
the reuse identifier of cell in a particular location.
@param configureCellBlock A block that will be called when the view asks for a
cell in a particular location.

@return An initialized data source.
*/
- (id)initWithItems:(NSArray *)items
reuseIdentifierBlock:(TGRDataSourceReuseIdentifierBlock)reuseIdentifierBlock
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock;

@end
17 changes: 16 additions & 1 deletion TGRDataSource/TGRArrayDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ - (id)initWithItems:(NSArray *)items
cellReuseIdentifier:(NSString *)reuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock
{
self = [super initWithCellReuseIdentifier:reuseIdentifier configureCellBlock:configureCellBlock];
self = [super initWithCellReuseIdentifier:reuseIdentifier
configureCellBlock:configureCellBlock];

if (self) {
_items = [items copy];
}

return self;
}

- (id)initWithItems:(NSArray *)items
reuseIdentifierBlock:(TGRDataSourceReuseIdentifierBlock)reuseIdentifierBlock
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock
{
self = [super initWithReuseIdentifierBlock:reuseIdentifierBlock
configureCellBlock:configureCellBlock];

if (self) {
_items = [items copy];
Expand Down
36 changes: 30 additions & 6 deletions TGRDataSource/TGRDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#import <UIKit/UIKit.h>

typedef NSString *(^TGRDataSourceReuseIdentifierBlock)(NSIndexPath *indexPath, id item);
typedef void (^TGRDataSourceCellBlock)(id cell, id item);

/**
Expand All @@ -31,26 +32,49 @@ typedef void (^TGRDataSourceCellBlock)(id cell, id item);
@interface TGRDataSource : NSObject <UITableViewDataSource, UICollectionViewDataSource>

/**
The cell reuse identifier.
A block that will be called when the view asks for the reuse identifier of cell in a
particular location.
*/
@property (copy, nonatomic, readonly) NSString *cellReuseIdentifier;
@property (copy, nonatomic, readonly) TGRDataSourceReuseIdentifierBlock reuseIdentifierBlock;

/**
A block that will be called when the view asks for a cell in a particular location.
*/
@property (copy, nonatomic, readonly) TGRDataSourceCellBlock configureCellBlock;

/**
Initializes the data source.
Initializes the data source using a single reuse identifier.

@param reuseIdentifier The cell reuse identifier.
@param configureCellBlock A block that will be called when the view asks for a cell in a particular location.
@discussion This initializer internally creates a custom reuse identifier block
that will return the default reuse identifier for all cells. This is useful
when a data source provide a single kind of cell, for homogeneous views.

@param defaultReuseIdentifier The default cell reuse identifier.
@param configureCellBlock A block that will be called when the view asks for a
cell in a particular location.

@return An initialized data source.
*/
- (id)initWithCellReuseIdentifier:(NSString *)reuseIdentifier
- (id)initWithCellReuseIdentifier:(NSString *)defaultReuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock;

/**
Initializes the data source with a custom cell reuse identifier block.

@discussion This is the designated initializer of this class. It creates
a data source that provides multiple kinds of cell that can be used in
the implementation of heterogeneous views.

@param reuseIdentifierBlock A block that will be called when the view asks for
the reuse identifier of cell in a particular location.
@param configureCellBlock A block that will be called when the view asks for a
cell in a particular location.

@return An initialized data source.
*/
- (id)initWithReuseIdentifierBlock:(TGRDataSourceReuseIdentifierBlock)reuseIdentifierBlock
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock;

/**
Returns a data source item in a particular location.
*/
Expand Down
49 changes: 37 additions & 12 deletions TGRDataSource/TGRDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,32 @@

@interface TGRDataSource ()

@property (copy, nonatomic) NSString *cellReuseIdentifier;
@property (copy, nonatomic) TGRDataSourceReuseIdentifierBlock reuseIdentifierBlock;
@property (copy, nonatomic) TGRDataSourceCellBlock configureCellBlock;

@end

@implementation TGRDataSource

- (id)initWithCellReuseIdentifier:(NSString *)reuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock
{
self = [super init];
- (id)initWithCellReuseIdentifier:(NSString *)defaultReuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock {

TGRDataSourceReuseIdentifierBlock defaultBlock = ^NSString *(NSIndexPath *indexPath, id item) {
return defaultReuseIdentifier;
};

self = [self initWithReuseIdentifierBlock:defaultBlock
configureCellBlock:configureCellBlock];

return self;
}

- (id)initWithReuseIdentifierBlock:(TGRDataSourceReuseIdentifierBlock)reuseIdentifierBlock
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock {

self = [super init];
if (self) {
self.cellReuseIdentifier = reuseIdentifier;
self.configureCellBlock = configureCellBlock;
self.reuseIdentifierBlock = reuseIdentifierBlock;
}

return self;
Expand All @@ -62,11 +73,19 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellReuseIdentifier
forIndexPath:indexPath];
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id item = [self itemAtIndexPath:indexPath];

NSString *reuseIdentifier = @"";
if (self.reuseIdentifierBlock) {
reuseIdentifier = self.reuseIdentifierBlock(indexPath, item);
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier
forIndexPath:indexPath];

if (self.configureCellBlock) {
self.configureCellBlock(cell, item);
}
Expand All @@ -86,10 +105,16 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellReuseIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];

NSString *reuseIdentifier = @"";
if (self.reuseIdentifierBlock) {
reuseIdentifier = self.reuseIdentifierBlock(indexPath, item);
}

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier
forIndexPath:indexPath];

if (self.configureCellBlock) {
self.configureCellBlock(cell, item);
}
Expand Down
18 changes: 17 additions & 1 deletion TGRDataSource/TGRFetchedResultsDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,28 @@

@param controller The `NSFetchedResultsController` object managed by this data source.
@param reuseIdentifier The cell reuse identifier.
@param configureCellBlock A block that will be called when the view asks for a cell in a particular location.
@param configureCellBlock A block that will be called when the view asks for a cell in
a particular location.

@return An initialized data source.
*/
- (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller
cellReuseIdentifier:(NSString *)reuseIdentifier
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock;

/**
Initializes the data source without a default cell reuse identifier.

@param controller The `NSFetchedResultsController` object managed by this data source.
@param reuseIdentifierBlock A block that will be called when the view asks for
the reuse identifier of cell in a particular location.
@param configureCellBlock A block that will be called when the view asks for a cell in
a particular location.

@return An initialized data source.
*/
- (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller
reuseIdentifierBlock:(TGRDataSourceReuseIdentifierBlock)reuseIdentifierBlock
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock;

@end
14 changes: 14 additions & 0 deletions TGRDataSource/TGRFetchedResultsDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ - (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller
return self;
}

- (id)initWithFetchedResultsController:(NSFetchedResultsController *)controller
reuseIdentifierBlock:(TGRDataSourceReuseIdentifierBlock)reuseIdentifierBlock
configureCellBlock:(TGRDataSourceCellBlock)configureCellBlock
{
self = [super initWithReuseIdentifierBlock:reuseIdentifierBlock
configureCellBlock:configureCellBlock];

if (self) {
_fetchedResultsController = controller;
}

return self;
}

- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
return [self.fetchedResultsController objectAtIndexPath:indexPath];
}
Expand Down