iOS中tableview支持不同种类的cell

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


屏幕快照 2017-01-08 03.43.03.png

准备工作

创建一个基类的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;
}

总结

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

最后

所有源码和Demo

如果您觉得有帮助,不妨给个star鼓励一下,欢迎关注&交流

有任何问题欢迎评论私信或者提issue

QQ:757765420
Email:nemocdz@gmail.com
Github:Nemocdz
微博:@Nemocdz

谢谢观看

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,086评论 3 38
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,205评论 30 471
  • 前言 由于最近两个多月,笔者正和小伙伴们忙于对公司新项目的开发,笔者主要负责项目整体架构的搭建以及功能模块的分工。...
    CoderMikeHe阅读 27,107评论 74 270
  • 我和海明威是老朋友了。自打他出生在美丽的瓦伦湖旁边,我就和他为伴,他对我也熟悉,我们无话不说,没事的时候经常摇曳在...
    淡定的懒懒阅读 430评论 0 0
  • 我们每个人都有梦想, 有着与众不同的梦想。 可以是红色热情的梦想, 可以是紫色浪漫的梦想, 也可以是绿色生命的梦想...
    薛舒阳阅读 204评论 0 4