UITableView“优雅”支持不同类型的Cell

在某些业务场景下,同一个UITableView需要支持多种UITableViewCell。考虑一下即时通信的聊天对话列表,不同的消息类型对应于不同的UITableViewCell,同一个tableview往往需要支持多达10种以上的cell类型,例如文本、图片、位置、红包等等。一般情况下,UITableViewCell往往会和业务数据模型绑定,来展示数据。根据不同的业务数据,对应不同的cell。本文将探讨如何有效的管理和加载多种类型的cell。

为了方便讨论,假设我们要实现一个员工管理系统。一个员工包含名字和头像。如果员工只有名字,则只展示名字,如果只有头像,则只展示头像,如果既有名字,又有头像,则需要既展示头像又展示名字。

我们用一个Person类表示员工,用三种不同的cell来处理不同的展示情况。

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, strong) NSString *avatar;

@end

/*负责展示只有名字的员工*/

@interface TextCell : BaseCell

- (void)setPerson:(Person *)p;

@end

/*负责展示只有头像的员工*/

@interface ImageCell : BaseCell

- (void)setPerson:(Person *)p;

@end

/*负责展示只有既有名字又有头像的员工*/

@interface TextImageCell : BaseCell

- (void)setPerson:(Person *)p;

@end

这三个类都继承了BaseCell,BaseCell继承UITableViewCell

@interface BaseCell : UITableViewCell

- (void)setPerson:(Person *)p;

@end

下面我们在UITableView的delegate来处理展示Cell

第一次尝试:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

BaseCell *cell;

NSString *cellIdentifier;

switch (p.showtype) {

case PersonShowText:

cellIdentifier = @"TextCell";

break;

case PersonShowAvatar:

cellIdentifier = @"PersonShowAvatar";

break;

case PersonShowTextAndAvatar:

cellIdentifier = @"PersonShowTextAndAvatar";

break;

default:

break;

}

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {

switch (p.showtype) {

case PersonShowText:

cell = [[TextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

break;

case PersonShowAvatar:

cell = [[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

break;

case PersonShowTextAndAvatar:

cell = [[TextImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

break;

default:

break;

}

}

[cell setPerson:p];

return cell;

}

这段代码实现了根据不同的业务模型选取和显示Cell的逻辑。但是这段代码包含了重复代码,switch case被调用了两次。我们改进一下代码:

第二次尝试

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

BaseCell *cell;

NSString *cellIdentifier;

Class cellClass;

switch (p.showtype) {

case PersonShowText:

cellClass = [TextCell class];

break;

case PersonShowAvatar:

cellClass = [ImageCell class];

break;

case PersonShowTextAndAvatar:

cellClass = [TextImageCell class];

break;

default:

cellClass = [UITableViewCell class];

break;

}

cellIdentifier = NSStringFromClass(cellClass);

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {

cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

[cell setPerson:p];

return cell;

}

这次比第一次的代码看起来好了不少,通过一个通用的Class对象,来动态生成cell,避免了两次调用switch case的重复代码。但是,有没有更好的实现方式?

第三次尝试

- (void)viewDidLoad

{

...

[self registerCell];  //注册cell

}

- (void)registerCell

{

[_tableView registerClass:[TextCell class] forCellReuseIdentifier:NSStringFromClass([TextCell class])];

[_tableView registerClass:[ImageCell class] forCellReuseIdentifier:NSStringFromClass([ImageCell class])];

[_tableView registerClass:[TextImageCell class] forCellReuseIdentifier:NSStringFromClass([TextImageCell class])];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

Person *p = _persons[indexPath.row];

BaseCell *cell;

NSString *cellIdentifier;

switch (p.showtype) {

case PersonShowText:

cellIdentifier = NSStringFromClass([TextCell class]);

break;

case PersonShowAvatar:

cellIdentifier = NSStringFromClass([ImageCell class]);

break;

case PersonShowTextAndAvatar:

cellIdentifier = NSStringFromClass([TextImageCell class]);

break;

default:

cellIdentifier = NSStringFromClass([UITableViewCell class]);

break;

}

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

[cell setPerson:p];

return cell;

}

可以看到,这次我们调用了- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier方法,把tableView和cell先配置好,并且在cellForRowAtIndexPath方法里面,去掉了if (!cell) {...}的处理,代码看起来更加简洁。

为什么不再需要判断cell是否为空?因为通过registerClass方法注册了cell之后,dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath方法会确保有一个可用的cell返回。

当然,我们可以把类型判断的这段代码提取出来,让cellForRowAtIndexPath方法看起来更加简洁

@interface Person : NSObject

......

@property (nonatomic, strong) NSString *cellIdentifier;

@end

@implementation Person

- (NSString *)cellIdentifier

{

if (_showtype == PersonShowTextAndAvatar) {

return NSStringFromClass([TextImageCell class]);

} else if (_showtype == PersonShowAvatar){

return NSStringFromClass([ImageCell class]);

} else {

return NSStringFromClass([TextCell class]);

}

}

@end

现在cellForRowAtIndexPath方法看起来就像下面这样,明显简洁多了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

Person *p = _persons[indexPath.row];

BaseCell *cell;

NSString *cellIdentifier;

cellIdentifier = p.cellIdentifier;

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

[cell setPerson:p];

return cell;

}

结论:

使用- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier和- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath可以让UITableView处理多种类型的cell更加灵活和轻松。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容