tableview 修改单元格内容字体大小_如何修改一次代码就可以完成多种类型 cell 的 UITableView 增删修改...

本文介绍了ZBCellConfig对象在管理UITableView中不同类型的cell内容时的应用,包括如何初始化配置数组、增删修改cell,以及实现cell自适应高度。通过ZBCellConfig,可以简化tableView代理方法的修改,提高代码维护性。
摘要由CSDN通过智能技术生成

e69cfb860d875868eb66b748317ddafe.png

前言

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

简介

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

结构

b5b6521156825191b4d8ec55e087aac0.png

使用

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

知识点


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

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

063b1da3caa31f9e5319d2d6edcb97b3.gif

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

dab9d79a179c29272766089a379458bd.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
82ac6781e149b475696a09cd0b1bf2b1.png
If I have no knife,I can't protect you.If I had a sword,I can't hold you. 如果我没有刀,我就不能保护你。如果我有刀,我就不能拥抱你。 ————《剪刀手爱德华》
好的,以下是一个简单的示例代码,演示如何在UITableView修改单元格内容: 首先,你需要实现UITableViewDataSource协议tableView(_:cellForRowAt:)方法,该方法用于配置单元格并返回UITableViewCell对象。 在这个方法,你可以设置单元格的文本标签、图像等属性。当你需要更改单元格内容时,可以通过访问单元格的标签和其他视图来执行此操作。 例如,下面的代码显示如何在单元格显示一个数字,并在用户点击单元格时将其增加1: ``` class MyTableViewController: UITableViewController { var numbers = [1, 2, 3, 4, 5] override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return numbers.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) // 设置单元格的文本标签为数字 cell.textLabel?.text = "\(numbers[indexPath.row])" return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 将单元格的数字增加1 numbers[indexPath.row] += 1 // 获取单元格并更新标签 let cell = tableView.cellForRow(at: indexPath) cell?.textLabel?.text = "\(numbers[indexPath.row])" } } ``` 在这个示例,我们首先在MyTableViewController类定义了一个numbers数组,用于存储要显示在UITableView的数字。在tableView(_:cellForRowAt:)方法,我们设置单元格的文本标签为相应的数字。 当用户点击单元格时,didSelectRowAt方法会被调用。在这个方法,我们将单元格的数字增加1,并使用tableView(_:cellForRow:)方法获取单元格并更新其标签。 需要注意的是,如果你想在UITableView更新单元格内容,你必须使用UITableViewcellForRow(at:)方法获取单元格对象,然后使用它来更新单元格内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值