• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ZBCellConfig: 针对 UITableView 含有多类型 Cell 的一套管理方案

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

ZBCellConfig

开源软件地址:

https://gitee.com/itzhangbao/ZBCellConfig

开源软件介绍:

当一个 tableView 中的 cell 类型过多时,我们务必会在 tableView 的各个代理中做这样那样的判断,当需要增加一种 cell,或调换 cell 的顺序的时候我们就会在 tableView 的各个代理中进行修改判断。使用 ZBCellConfig 可以应对各种变态需求,当增删、调换 cell 的顺序时,只需一键配置。

简介

  • ZBCellConfig 对象实例会将 tableView 中 cell 所需的基本信息存储下来,然后放到数组中进行管理;
  • 每个 ZBCellConfig 实例与 tableView 中想要显示的 cell 相对应。(但注意,是"想要显示的"cell,由于cell的重用,实际上cell并不会生成那么多);
  • 优点:改变不同类型 cell 的顺序、增删时,极为方便,只需改变用于存放 ZBCellConfig 的数组即可,** 重点是无需在多个tableView代理方法中逐个修改 **。

结构

目录结构

使用

  • 支持 cocoapods 导入 pod 'ZBCellConfig'
  • 直接讲文件拖拽到项目中 #import "ZBCellConfig.h"

知识点

请下载示例项目查看详细使用方法及实际中如何使用 GitHub下载地址。初、中、高、高 MVVM 内容上是一样的,区别在于没一级别知识点递增。

Demo 展示

知识点 - 基本使用

1 . 首先在控制器中声明存放 ZBCellConfig 实例的二维数组

/*** 二维数组 (匹配 tableView 的数据结构,第一层是 section,第二层放 cell)*/@property (nonatomic, strong) NSMutableArray <NSArray <ZBCellConfig *> *> * cellConfigs;

2-1 . 初始化数组,每一个 ZBCellConfig 为 cell 的基本信息,改变不同类型cell的顺序、增删时,只需在此修改即可,无需在多个tableView代理方法中逐个修改(具体查看 Demo 注释很清晰)

-  (NSMutableArray<NSArray<ZBCellConfig *> *> *)cellConfigs {    _cellConfigs = [[NSMutableArray alloc] init];    // cell1    ZBCellConfig *cell1Config = [ZBCellConfig cellConfigWithClass:[LowTableViewCell1 class] showCellInfoMethod:@selector(setModel:)];    [_cellConfigs addObject:@[cell1Config]];    // cell2    ZBCellConfig *cell2Config = [ZBCellConfig cellConfigWithClass:[LowTableViewCell1 class] showCellInfoMethod:@selector(setModel:)];    [_cellConfigs addObject:@[cell2Config]];    // cell3    ....    return _cellConfigs;}

2-2 . 增删只需这样:

增删操作.gif效果图.gif

3 . tableView 代理中实现部分

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return self.cellConfigs.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.cellConfigs[section] count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 根据 indexPath 获取 对应的 cellConfig    ZBCellConfig *cellConfig = self.cellConfigs[indexPath.section][indexPath.row];    // 根据对应的 cellConfig 获取 cell,并给 cell 赋值 根据模型显示。    // • dataModels: 这里由于为示例代码不是用真实数据,只起到 执行 cell 的赋值函数。在实际项目中应该传递从网络请求的真实数据。    UITableViewCell *cell = [cellConfig cellOfCellConfigWithTableView:tableView dataModels:@[[LowModel new]]];    return cell;}

知识点 - cell 自适应高度

1 . tableView 设置如下

/*** default is 0, which means there is no estimate* estimatedRowHeight 默认为 0,不估算cell高度* 赋值不为 0 时候,开启cell估值配合 layout 约束,进行cell高度自适应* 也就是说想要自动布局 cell 高度就给这个 estimatedRowHeight 属性赋值,值为你所有 cell 的平均高度的一个估值*/_heightTableView.estimatedRowHeight = 100;// iOS8 系统中 rowHeight 的默认值已经设置成了 UITableViewAutomaticDimension_heightTableView.rowHeight = UITableViewAutomaticDimension;

2 . cell 需采用 AutoLayout 布局,masory 或 xib 托线的形式皆可,约束规定上左下右还有让 cell 知道内容的高:AutoLayout约束

知识点 - tableView style

  1. UITableViewStylePlain 和 UITableViewStyleGrouped 区别
  2. UITableViewStylePlain:sectionView 当 tableView cectionHeader/Footer 会默认高度为0, 滑动到顶部时 会停留到导航栏底部
  3. UITableViewStyleGrouped:sectionView 当 tableView cectionHeader/Footer 会默认高度为10, 滑动到顶部时 不会停留到导航栏底部
  4. 注意:当需要 为 UITableViewStyleGrouped 时查看【MediaExample使用示例】

知识点 - ZBCellConfig

通过函数 - (void)zb_performSelector:(SEL)aSelector withObjects:(NSArray *)objects来执行 初始化时配置好的showCellInfoMethod。此函数是对 - (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;的一个扩展,以数组的形式来传递多参数。函数实现部分:

    // 签名    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:aSelector];    if (signature == nil) {        NSAssert(false, @"LINE=%d ERROR - 找不到 %@ 方法", __LINE__ ,NSStringFromSelector(aSelector));    }    // 包装    NSInvocation *invocation  = [NSInvocation invocationWithMethodSignature:signature];    // 设置调用者    [invocation setTarget:self];    // 设置调用的方法 与 NSMethodSignature 签名的方法一致    [invocation setSelector:aSelector];    // 0为target 1为_cmd 所以从2索引    for (int i = 0; i < (signature.numberOfArguments - 2); i++) {        id dataModel = i < objects.count ? objects[i] : nil;        [invocation setArgument:&dataModel atIndex:i+2];    }    // retain 所有参数,防止释放    [invocation retainArguments];    [invocation invoke];

总结

把将要展示的几种 cell的基本信息 以 tableView 的数据结构二维数组的形式存放,储存基本配置并不会生成多余的 cell,内部创建采用 tableView 的重用机制。示例 Demo 中注释详细。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
fenglu-cordova-wxpay: 微信支付cordova插件发布时间:2022-03-24
下一篇:
mars: 面向亿万用户级的移动端Web解决方案发布时间:2022-03-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap