在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ZBCellConfig开源软件地址:https://gitee.com/itzhangbao/ZBCellConfig开源软件介绍:
简介
结构使用
知识点
知识点 - 基本使用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 . 增删只需这样: 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 知道内容的高: 知识点 - tableView style
知识点 - ZBCellConfig通过函数 // 签名 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]; 总结
|
请发表评论