qt tableview修改表格内容_如何修改一次代码就可以完成多种类型 cell 的 UITableView 增删修改

51a5721112888be164b6601a754177c8.png

前言

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

简介

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

结构

a0c11c1bf970595c451e1af9edf79aaa.png

使用

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

知识点


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

e436ef99f204a3c29b741e0e27c8eb7b.png

基本使用

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 . 增删只需这样:

18668f75bf4101e1e9fa5d197a2645bc.png

030aa5a4107bd67244df2b381ba9f78a.png

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 知道内容的高:

77bd32a5c0793ea959b30c00f76f51f8.png

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 中注释详细,
itzhangbao/ZBCellConfig​github.com
7c1247665308318f73aaa8ab38e4572c.png
If I have no knife,I can't protect you.If I had a sword,I can't hold you. 如果我没有刀,我就不能保护你。如果我有刀,我就不能拥抱你。 ————《剪刀手爱德华》
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值