美文网首页
封装的tableView,瘦身VC

封装的tableView,瘦身VC

作者: Hflydragon | 来源:发表于2017-12-21 11:23 被阅读0次

导语:

通常我们在开发中,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项目中尤为方便。

相关文章

网友评论

      本文标题:封装的tableView,瘦身VC

      本文链接:https://www.haomeiwen.com/subject/kuexvxtx.html