iOS如何让tableview支持不同种类的cell详解

2022-10-21

这篇文章主要给大家介绍了关于iOS如何让tableview支持不同种类的cell的相关资料,文中通过示例代码详细的给大家介绍了实现的两种方法,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

我们在项目中偶尔需要让tableview里支持不同种类的cell,比如微博的原创微博和别人转发的微博,就是两种cell。又或是类似支付宝的的timeline也有各种类型的cell。在同一个tableview里实现不同种类的cell,一般有两种方法,一种是把所有种类的cell先注册了,再根据不同的identifer去加载cell,一种是在init时创建不同的identifer的cell。

效果图如下:

准备工作

创建一个基类的CDZBaseCell,基类cell拥有一些共用的属性和方法,如持有model,解析model。

创建不同的子类cell,以两个子类CDZTypeACell CDZTypeBCell 为例,继承自CDZBaseCell,重写一些方法,如高度,显示视图等等。

Datasource中准备好判断index所在的cell种类的方法(如根据model的type属性等)

- (Class)cellClassAtIndexPath:(NSIndexPath *)indexPath{
 CDZTableviewItem *item = [self itemAtIndexPath:indexPath];
 switch (item.type) {
  case typeA:{
   return [CDZTypeACell class];
  }
   break;
  case typeB:{
   return [CDZTypeBCell class];
  }
   break;
 }
}

- (CDZTableviewItem *)itemAtIndexPath:(NSIndexPath *)indexPath{
 return self.itemsArray[indexPath.row];
}

- (NSString *)cellIdentiferAtIndexPath:(NSIndexPath *)indexPath{
 return NSStringFromClass([self cellClassAtIndexPath:indexPath]);
}

方法一:先注册,根据identifer去加载不同的cell

先在tableview创建时注册需要的不同种类,再判断index对应的种类,再根据identifer加载子类cell。

[self.tableview registerClass:[CDZTypeACell class] forCellReuseIdentifier:NSStringFromClass([CDZTypeBCell class])];
[self.tableView registerClass:[CDZTypeBCell class] forCellReuseIdentifier:NSStringFromClass([CDZTypeBCell class])];

并在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中根据重用标识加载cell。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 CDZBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:[self cellIdentiferAtIndexPath:indexPath] forIndexPath:indexPath];
 cell.item = [self itemAtIndexPath:indexPath];
 return cell;
}

方法二:在init时创建不同identifer的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中判断cell是否为nil,并根据index所在cell的种类初始化cell和其identifer。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 CDZBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:[self cellIdentiferAtIndexPath:indexPath]];
 if (!cell) {
  Class cls = [self cellClassAtIndexPath:indexPath];
  cell = [[cls alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self cellIdentiferAtIndexPath:indexPath]];
 }
 cell.item = [self itemAtIndexPath:indexPath];
 return cell;
}

源码下载

所有源码和Demo(本地下载)

总结

个人更喜欢第二种,苹果官方文档也推荐第二种方法去重用cell。我觉得优点是一个是在tableview划分MVC架构时,tableview创建时不需要知道cell的类型,而只需要知道datasouce,而datasource才是需要去分配cell类型的。第二个是tableviewcell的初始化方法并非只能用initWithStyle(collectionview必须先注册的原因则在于初始化方法只有initWithFrame)。而使用了注册,则是在复用池空时默认调用initWithStyle的方法,如果需要用别的方法初始化就不可以了。第一种方法可以用在有一些库需要先注册后才能调用的,比如自动计算cell高度的库FDTemplateLayoutCell。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对北冥有鱼的支持。

您可能感兴趣的文章:

  • iOS触摸事件UITouch应用详解
  • iOS二维码的生成代码
  • IOS中的target action控件的实现
  • iOS获取某个日期后n个月的日期
  • iOS实现支持小数的星星评分组件实例代码
  • React Native第三方平台分享的实例(Android,IOS双平台)
  • IOS 开发之ObjectiveC的变量类型的字符代表

《iOS如何让tableview支持不同种类的cell详解.doc》

下载本文的Word格式文档,以方便收藏与打印。