通过分离dataSource 让我们的code具有更高的复用性.

转载自汪海的实验室

定义dataSource

dataSource.h

[objc]  view plain  copy
  1. typedef void (^TableViewCellConfigureBlock)(id cell, id item);  
  2.   
  3. @interface GroupNotificationDataSource : NSObject<UITableViewDataSource>  
  4.   
  5. - (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;  
  6.   
  7. - (id)itemAtIndexPath:(NSIndexPath *)indexPath;  
  8.   
  9. @end  

dataSource.m

[objc]  view plain  copy
  1. @implementation GroupNotificationDataSource  
  2. {  
  3.     NSArray     *_itemsArray;  
  4.     NSString    *_cellIdentifier;  
  5.     TableViewCellConfigureBlock _configureCellBlock;  
  6. }  
  7.   
  8. - (id)init{  
  9.     return nil;  
  10. }  
  11.   
  12. - (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSString *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock{  
  13.     self = [super init];  
  14.     if (self) {  
  15.         _itemsArray = anItems;  
  16.         _cellIdentifier = aCellIdentifier;  
  17.         _configureCellBlock = [aConfigureCellBlock copy];  
  18.     }  
  19.     return self;  
  20. }  
  21.   
  22. - (id)itemAtIndexPath:(NSIndexPath *)indexPath {  
  23.     return _itemsArray[(NSUInteger) indexPath.row];  
  24. }  
  25.   
  26. #pragma mark tableView DataSource  
  27. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  28.     return _itemsArray.count;  
  29. }  
  30.   
  31. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  32.     // NOTE: This method can return nil so you need to account for that in code   
  33.     GroupNotificationCell *cell=[tableView dequeueReusableCellWithIdentifier:_cellIdentifier];  
  34.     // NOTE: Add some code like this to create a new cell if there are none to reuse  
  35.     if (cell==nil) {  
  36.         cell=[[GroupNotificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_cellIdentifier];  
  37.     }  
  38.     id item = [self itemAtIndexPath:indexPath];  
  39.     _configureCellBlock(cell, item);  
  40.     return cell;  
  41. }  

其中,需要注意tableView:cellForRowAtIndexPath:方法中对于cell的重用

重写cell的初始化

cell.h

[objc]  view plain  copy
  1. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;  

cell.m

[objc]  view plain  copy
  1. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{  
  2.     self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  3.     if (self) {  
  4.         [self configureUI];  
  5.     }  
  6.     return self;  
  7. }  

controller中设置tableview的dataSource

[objc]  view plain  copy
  1. _dataSource=[[GroupNotificationDataSource alloc]initWithItems:[NSArray arrayWithObjects:@"1",@"2", nil nil] cellIdentifier:CellIdentifier configureCellBlock:^(id cell, id item) {  
  2.         //  
  3.     }];  
  4.     _tableview.dataSource=_dataSource;  

需要注意的是_dataSource应该是成员变量,而非局部变量,否则会报错:message send to delloced object
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值