UITableView“優(yōu)雅”支持不同類型的Cell

在某些業(yè)務(wù)場(chǎng)景下,同一個(gè)UITableView需要支持多種UITableViewCell宠纯《孵铮考慮一下即時(shí)通信的聊天對(duì)話列表,不同的消息類型對(duì)應(yīng)于不同的UITableViewCell衬浑,同一個(gè)tableview往往需要支持多達(dá)10種以上的cell類型捌浩,例如文本、圖片工秩、位置尸饺、紅包等等。一般情況下助币,UITableViewCell往往會(huì)和業(yè)務(wù)數(shù)據(jù)模型綁定浪听,來展示數(shù)據(jù)。根據(jù)不同的業(yè)務(wù)數(shù)據(jù)眉菱,對(duì)應(yīng)不同的cell迹栓。本文將探討如何有效的管理和加載多種類型的cell。

為了方便討論俭缓,假設(shè)我們要實(shí)現(xiàn)一個(gè)員工管理系統(tǒng)克伊。一個(gè)員工包含名字和頭像。如果員工只有名字华坦,則只展示名字愿吹,如果只有頭像,則只展示頭像惜姐,如果既有名字犁跪,又有頭像,則需要既展示頭像又展示名字歹袁。

我們用一個(gè)Person類表示員工坷衍,用三種不同的cell來處理不同的展示情況。

@interface?Person?:?NSObject

@property?(nonatomic,?copy)?NSString?*name;

@property?(nonatomic,?strong)?NSString?*avatar;

@end

/*負(fù)責(zé)展示只有名字的員工*/

@interface?TextCell?:?BaseCell

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

@end

/*負(fù)責(zé)展示只有頭像的員工*/

@interface?ImageCell?:?BaseCell

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

@end

/*負(fù)責(zé)展示只有既有名字又有頭像的員工*/

@interface?TextImageCell?:?BaseCell

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

@end

這三個(gè)類都繼承了BaseCell条舔,BaseCell繼承UITableViewCell

@interface?BaseCell?:?UITableViewCell

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

@end

下面我們?cè)赨ITableView的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;

}

這段代碼實(shí)現(xiàn)了根據(jù)不同的業(yè)務(wù)模型選取和顯示Cell的邏輯惫叛。但是這段代碼包含了重復(fù)代碼,switch case被調(diào)用了兩次逞刷。我們改進(jìn)一下代碼:

第二次嘗試

-?(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;

}

這次比第一次的代碼看起來好了不少嘉涌,通過一個(gè)通用的Class對(duì)象,來動(dòng)態(tài)生成cell夸浅,避免了兩次調(diào)用switch case的重復(fù)代碼仑最。但是,有沒有更好的實(shí)現(xiàn)方式帆喇?

第三次嘗試

-?(void)viewDidLoad

{

...

[self?registerCell];??//注冊(cè)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;

}

可以看到警医,這次我們調(diào)用了- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier方法,把tableView和cell先配置好,并且在cellForRowAtIndexPath方法里面预皇,去掉了if (!cell) {...}的處理侈玄,代碼看起來更加簡(jiǎn)潔。

為什么不再需要判斷cell是否為空吟温?因?yàn)橥ㄟ^registerClass方法注冊(cè)了cell之后序仙,dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath方法會(huì)確保有一個(gè)可用的cell返回。

當(dāng)然鲁豪,我們可以把類型判斷的這段代碼提取出來潘悼,讓cellForRowAtIndexPath方法看起來更加簡(jiǎn)潔

@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

現(xiàn)在cellForRowAtIndexPath方法看起來就像下面這樣,明顯簡(jiǎn)潔多了

-?(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;

}

結(jié)論:

使用- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier和- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath可以讓UITableView處理多種類型的cell更加靈活和輕松爬橡。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末治唤,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子糙申,更是在濱河造成了極大的恐慌宾添,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柜裸,死亡現(xiàn)場(chǎng)離奇詭異辞槐,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)粘室,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卜范,“玉大人衔统,你說我怎么就攤上這事『Q” “怎么了锦爵?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)奥裸。 經(jīng)常有香客問我险掀,道長(zhǎng),這世上最難降的妖魔是什么湾宙? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任樟氢,我火速辦了婚禮,結(jié)果婚禮上侠鳄,老公的妹妹穿的比我還像新娘埠啃。我一直安慰自己,他們只是感情好伟恶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布碴开。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪潦牛。 梳的紋絲不亂的頭發(fā)上眶掌,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音巴碗,去河邊找鬼朴爬。 笑死,一個(gè)胖子當(dāng)著我的面吹牛良价,可吹牛的內(nèi)容都是我干的寝殴。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼明垢,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼蚣常!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起痊银,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤抵蚊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后溯革,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贞绳,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年致稀,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了冈闭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抖单,死狀恐怖萎攒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情矛绘,我是刑警寧澤耍休,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站货矮,受9級(jí)特大地震影響羊精,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜囚玫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一喧锦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧抓督,春花似錦裸违、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春怔昨,著一層夾襖步出監(jiān)牢的瞬間雀久,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工趁舀, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赖捌,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓矮烹,卻偏偏與公主長(zhǎng)得像越庇,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子奉狈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容