diff --git a/TGRDataSource/TGRArrayDataSource.h b/TGRDataSource/TGRArrayDataSource.h index b4608a4..69e8372 100644 --- a/TGRDataSource/TGRArrayDataSource.h +++ b/TGRDataSource/TGRArrayDataSource.h @@ -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. @param configureCellBlock A block that will be called when the view asks for a cell in a particular location. @return An initialized data source. @@ -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 diff --git a/TGRDataSource/TGRArrayDataSource.m b/TGRDataSource/TGRArrayDataSource.m index 0ad6a15..a67c7b8 100644 --- a/TGRDataSource/TGRArrayDataSource.m +++ b/TGRDataSource/TGRArrayDataSource.m @@ -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]; diff --git a/TGRDataSource/TGRDataSource.h b/TGRDataSource/TGRDataSource.h index c1ba386..53f2766 100644 --- a/TGRDataSource/TGRDataSource.h +++ b/TGRDataSource/TGRDataSource.h @@ -22,6 +22,7 @@ #import +typedef NSString *(^TGRDataSourceReuseIdentifierBlock)(NSIndexPath *indexPath, id item); typedef void (^TGRDataSourceCellBlock)(id cell, id item); /** @@ -31,9 +32,10 @@ typedef void (^TGRDataSourceCellBlock)(id cell, id item); @interface TGRDataSource : NSObject /** - 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. @@ -41,16 +43,38 @@ typedef void (^TGRDataSourceCellBlock)(id cell, id item); @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. */ diff --git a/TGRDataSource/TGRDataSource.m b/TGRDataSource/TGRDataSource.m index 4b3d90c..b34591e 100644 --- a/TGRDataSource/TGRDataSource.m +++ b/TGRDataSource/TGRDataSource.m @@ -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; @@ -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); } @@ -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); } diff --git a/TGRDataSource/TGRFetchedResultsDataSource.h b/TGRDataSource/TGRFetchedResultsDataSource.h index 33223d6..9e6fb6e 100644 --- a/TGRDataSource/TGRFetchedResultsDataSource.h +++ b/TGRDataSource/TGRFetchedResultsDataSource.h @@ -39,7 +39,8 @@ @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. */ @@ -47,4 +48,19 @@ 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 diff --git a/TGRDataSource/TGRFetchedResultsDataSource.m b/TGRDataSource/TGRFetchedResultsDataSource.m index d5b6d13..9d56bce 100644 --- a/TGRDataSource/TGRFetchedResultsDataSource.m +++ b/TGRDataSource/TGRFetchedResultsDataSource.m @@ -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]; }