Skip to content

Commit 73e66a4

Browse files
committed
Add delegate method to get reuse identifier for item
1 parent 1383eff commit 73e66a4

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

TGRDataSource/TGRDataSource.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,33 @@
2424

2525
typedef void (^TGRDataSourceCellBlock)(id cell, id item);
2626

27+
@class TGRDataSource;
28+
@protocol TDRDataSourceDelegate <NSObject>
29+
30+
@optional
31+
/**
32+
* Gets reuse identifier for cell when cellReuseIdentifier property is nil.
33+
*
34+
* @param dataSource The current data source
35+
* @param item The data source item
36+
*
37+
* @return The reuse identifier for data source item
38+
*/
39+
- (NSString *)dataSource:(TGRDataSource *)dataSource reuseIdentifierForItem:(id)item;
40+
41+
@end
42+
2743
/**
2844
Convenience class to encapsulate an `UITableView` or `UICollectionView` data source.
2945
Inspired by http://www.objc.io/issue-1/lighter-view-controllers.html
3046
*/
3147
@interface TGRDataSource : NSObject <UITableViewDataSource, UICollectionViewDataSource>
3248

49+
/**
50+
* The data source delegate
51+
*/
52+
@property (weak, nonatomic) id<TDRDataSourceDelegate>delegate;
53+
3354
/**
3455
The cell reuse identifier.
3556
*/

TGRDataSource/TGRDataSource.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
6363
}
6464

6565
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
66-
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellReuseIdentifier
67-
forIndexPath:indexPath];
6866
id item = [self itemAtIndexPath:indexPath];
67+
NSString * reuseIdentifier = self.cellReuseIdentifier;
68+
if (!reuseIdentifier)
69+
reuseIdentifier = [self.delegate dataSource:self reuseIdentifierForItem:item];
70+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier
71+
forIndexPath:indexPath];
6972

7073
if (self.configureCellBlock) {
7174
self.configureCellBlock(cell, item);
@@ -86,9 +89,12 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView
8689
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
8790
cellForItemAtIndexPath:(NSIndexPath *)indexPath
8891
{
89-
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellReuseIdentifier
90-
forIndexPath:indexPath];
9192
id item = [self itemAtIndexPath:indexPath];
93+
NSString * reuseIdentifier = self.cellReuseIdentifier;
94+
if (!reuseIdentifier)
95+
reuseIdentifier = [self.delegate dataSource:self reuseIdentifierForItem:item];
96+
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier
97+
forIndexPath:indexPath];
9298

9399
if (self.configureCellBlock) {
94100
self.configureCellBlock(cell, item);

0 commit comments

Comments
 (0)