美文网首页
UITableView 解耦

UITableView 解耦

作者: Ian_ | 来源:发表于2017-05-09 12:21 被阅读220次

产品开发的时候无法避免的就是在一个 UITableView中, 包含多种样式的 cell, 我们通常的做法如下:

if (indexPath.row == 0) {
      
} else if (indexPath.row == 1) {

} else if (indexPath.row == 2) {

} .....

这样的做法没有问题, 但是如果有很多种 cell 的话, 例如资料页, 当需要在第二和第三个 cell 之间插入一个 cell 的时候, 我们需要把数组添加一列, 然后把整个 if 改一遍, 如何避免 index 之间的耦合?

OK, 简单来说就是在数据模型 中添加一个标识, 数据的排列交给数组, 我们不判断当前是第几个, 只根据标识来确定放那种样式的cell,
这样只有当增加新的样式的时候才会涉及到改 UI 部分的代码, 大部分情况我们只需要对数组进行排序即可, 而不必因为该数组, 从而再改 if.

下面来介绍一款专门为了解耦而写的第三方: HYTableViewSection

使用方法:
pod 'HYTableViewSection'

通过构建数据来实现布局

    HYTableViewRow *row = [HYTableViewRow row:@{@"title":@"style1"}];
    row.identifier = @"style1";
    row.heightBlock = ^CGFloat{
        return 100;  // heightBlock 可以使用计算复杂高度, 或者直接使用 row.height
    };
    [self.tableView.hy_section addRowModel:row atSection:0];
    
    HYTableViewRow *row1 = [HYTableViewRow row:@{@"title":@"style2"}];
    row1.identifier = @"style2";
    row1.heightBlock = ^CGFloat{
        return 150;
    };
    [self.tableView.hy_section addRowModel:row1 atSection:0];
    
    
    HYTableViewRow *row2 = [HYTableViewRow row:@{@"title":@"style3"}];
    row2.identifier = @"style3";
    row2.heightBlock = ^CGFloat{
        return 80;
    };
    [self.tableView.hy_section addRowModel:row2 atSection:1];

代理方法中使用技巧, 除了新增 style 几乎不会涉及改 UITableView 的代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HYTableViewRow *row = [self.tableView.hy_section modelAtIndexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:row.identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:row.identifier];
    }
    cell.textLabel.text = row.model[@"title"];
    if ([row.identifier isEqualToString:@"style1"]) {
        cell.contentView.backgroundColor = [UIColor redColor];
    } else if ([row.identifier isEqualToString:@"style2"]) {
        cell.contentView.backgroundColor = [UIColor greenColor];
    } else if ([row.identifier isEqualToString:@"style3"]) {
        cell.contentView.backgroundColor = [UIColor blueColor];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HYTableViewRow *row = [self.tableView.hy_section modelAtIndexPath:indexPath];
    return row.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.tableView.hy_section numberOfRowsInSection:section];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.tableView.hy_section numberOfSections];
}

相关文章

  • UITableView 解耦

    产品开发的时候无法避免的就是在一个 UITableView中, 包含多种样式的 cell, 我们通常的做法如下: ...

  • iOS 解耦 --- 利用ViewModel沟通 View 与

    前言:之前利用过 ViewModel 进行过 UITableView 相关的业务解耦。新年后,结合曾经看过的一个网...

  • 解耦

    解耦 对于大型重构, 最有效的手段就是 解耦, 解耦的目的使实现代码高聚合、松耦合。 解耦为何如此...

  • 20171127-03问题整理

    总摘要: 解耦. 接口降低. 架构. 2017-11-27摘要: 解耦. 接口降低. 架构. 1.为什么说解耦的...

  • 如何优雅的对UITableView进行解耦

    在本文之前笔者已经将相关代码开源到 GitHub 上并添加了 CocoaPods 的支持,欢迎大家下载查看:STD...

  • UITableView超级解耦--模块化框架

    背景 iOS原生开发中,VC逻辑过于臃肿的case中,有相当的部分是来自UITableView的复杂逻辑难以抽离;...

  • 解耦

    今天小董给大家做了一个关于DMTP的精彩演讲,DMTP种种优秀的特性令我们这些听众叹为观止。 为何DMTP具有如此...

  • 解耦

    利用配置文件实现解耦 存在一个接口B 有3个实现类B1、B2、B3 在A类方法中需要调用B类实现代码: 此情况下,...

  • DDD 中的事件总线 (一)

    DDD 为什么需要event bus? 简单的说就是 解耦 ,无论是本地聚合间的解耦,还是微服务间的解耦. 没有e...

  • AFNetworking源码之AFAutoPurgingImag

    AFAutoPurgingImageCache图片缓存 通过2个protocol解耦,通过协议继承来解耦。协议相当...

网友评论

      本文标题:UITableView 解耦

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