封装的tableView,瘦身VC

导语:

通常我们在开发中,90%的时间都是在写一些重复的垃圾代码,真正的写代码时间其实只有5%左右的时间,而tableView却是我们最常用的控件,也是复用率最高的控件,那么简化tableView代码就显得尤为重要。

简化tableView在控制器VC中的代码

而通常我们最直接的简化代码操作是继承父类,把公共代码放在父类控制器中,这是最简单也是最有效的。还有一种是写一个分类,在分类中写公共方法,这在写tableView是将会用到。

通常在写VC代码时,自定义各种cell,避免不了重复无休止的写一些没有任何营养的代码 如

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiscoveryMenuContentView *cell = [tableView dequeueReusableCellWithIdentifier:[DiscoveryMenuContentView identifier] forIndexPath:indexPath];
            WS(weakSelf)
    cell.infoModelList = self.disoveryModel.infoList;
    return cell;
}

这将浪费了大量的时间去写相似代码,而且代码重用率非常低,耦合性太高。所以我们这里需要将能抽取出来的代码全部都抽取出来。

只需要写

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return [tableView dequeueReusableCellAndLoadDataWithAdapter:self.dataArray[indexPath.row] delegate:self indexPath:indexPath];
}

就能够使用于任何场景,而可以将这段代码写到父类中 所有的子类控制器只需要继承或者重写自定义即可。

而其实这段代码只是写在了tableView的分类中

@implementation UITableView (CustomCell)

- (CustomCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)adapter indexPath:(NSIndexPath *)indexPath {
   
   CustomCell *cell = [self dequeueReusableCellWithIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath];
   [cell loadContentWithAdapter:adapter delegate:nil tableView:self indexPath:indexPath];
   
   return cell;
}

- (CustomCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)adapter
                                                delegate:(id <CustomCellDelegate>)delegate
                                               indexPath:(NSIndexPath *)indexPath {
   
   CustomCell *cell = [self dequeueReusableCellWithIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath];
   [cell loadContentWithAdapter:adapter delegate:delegate tableView:self indexPath:indexPath];
   
   return cell;
}

抽取出来的只是dequeue的代码及数据的一些赋值代码,但是却大大提高了效率


赋值操作则是写在了adapter文件中(即分类中的CellDataAdapter),adapter文件之抽取出来的是一个抽象类,当中有cell的赋值调用,cell数据源及cell高度等缓存一些操作。
而adapter是封装了数据model的一个类,可以像添加model一样添加到数据源数组中

+ (CellDataAdapter *)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data
                                                cellHeight:(CGFloat)cellHeight cellType:(NSInteger)cellType {
   
   CellDataAdapter *adapter    = [[self class] new];
   adapter.cellReuseIdentifier = cellReuseIdentifiers;
   adapter.data                = data;
   adapter.cellHeight          = cellHeight;
   adapter.cellType            = cellType;
   
   return adapter;
}

+ (CellDataAdapter *)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data
                                                cellHeight:(CGFloat)cellHeight cellWidth:(CGFloat)cellWidth
                                                  cellType:(NSInteger)cellType {
   
   CellDataAdapter *adapter    = [[self class] new];
   adapter.cellReuseIdentifier = cellReuseIdentifiers;
   adapter.data                = data;
   adapter.cellHeight          = cellHeight;
   adapter.cellWidth           = cellWidth;
   adapter.cellType            = cellType;
   
   return adapter;
}

我们在调用的时候是可以直接赋值的

for (int i=0;i<result.count;i++) 
{
  [self.dataArray addObject:[CustomCell dataAdapterWithData:result[i]]];
}

在继承自CustomCell的类中直接可以赋值操作,大大简化了代码,解耦。在中小型及多table项目中尤为方便。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,735评论 4 61
  • 爱之深就是当你看见他和别的女孩在一起说说笑笑的时候你会很难过,、很想问他们在说什么! 爱之深就是每次吃饭、或去...
    yeah米阅读 984评论 0 0
  • 小时候,除了电视剧和书里,很少有人会真的说诸如温柔、温暖之类的词,仿佛这种词就像是一个幻影,镜花水月,即便有时候呼...
    庄清嘉阅读 3,899评论 0 0
  • 今天,与淘淘共读《妖精的小孩》第二部。这一部分主要讲述被妖精世界驱逐的萨思琪,在人类世界也受到排挤和非议,所...
    铿锵玫瑰llx阅读 4,235评论 0 0
  • 現在是午夜12:50分,但是你還未歸家。 為了迎接工作上的新挑戰,你已加班無數個夜晚。連最愛的足球也從之前一個禮拜...
    Marryanne阅读 1,787评论 0 0

友情链接更多精彩内容