国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS如何讓tableview支持不同種類的cell詳解

2019-10-21 18:45:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

我們?cè)陧?xiàng)目中偶爾需要讓tableview里支持不同種類的cell,比如微博的原創(chuàng)微博和別人轉(zhuǎn)發(fā)的微博,就是兩種cell。又或是類似支付寶的的timeline也有各種類型的cell。在同一個(gè)tableview里實(shí)現(xiàn)不同種類的cell,一般有兩種方法,一種是把所有種類的cell先注冊(cè)了,再根據(jù)不同的identifer去加載cell,一種是在init時(shí)創(chuàng)建不同的identifer的cell。

效果圖如下:

ios,tableviewcell,自定義tableviewcell,cell嵌套tableview

準(zhǔn)備工作

創(chuàng)建一個(gè)基類的CDZBaseCell,基類cell擁有一些共用的屬性和方法,如持有model,解析model。

創(chuàng)建不同的子類cell,以兩個(gè)子類CDZTypeACell CDZTypeBCell 為例,繼承自CDZBaseCell,重寫(xiě)一些方法,如高度,顯示視圖等等。

Datasource中準(zhǔn)備好判斷index所在的cell種類的方法(如根據(jù)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]);}

方法一:先注冊(cè),根據(jù)identifer去加載不同的cell

先在tableview創(chuàng)建時(shí)注冊(cè)需要的不同種類,再判斷index對(duì)應(yīng)的種類,再根據(jù)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中根據(jù)重用標(biāo)識(shí)加載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時(shí)創(chuàng)建不同identifer的cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中判斷cell是否為nil,并根據(jù)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本地下載

總結(jié)

個(gè)人更喜歡第二種,蘋(píng)果官方文檔也推薦第二種方法去重用cell。我覺(jué)得優(yōu)點(diǎn)是一個(gè)是在tableview劃分MVC架構(gòu)時(shí),tableview創(chuàng)建時(shí)不需要知道cell的類型,而只需要知道datasouce,而datasource才是需要去分配cell類型的。第二個(gè)是tableviewcell的初始化方法并非只能用initWithStyle(collectionview必須先注冊(cè)的原因則在于初始化方法只有initWithFrame)。而使用了注冊(cè),則是在復(fù)用池空時(shí)默認(rèn)調(diào)用initWithStyle的方法,如果需要用別的方法初始化就不可以了。第一種方法可以用在有一些庫(kù)需要先注冊(cè)后才能調(diào)用的,比如自動(dòng)計(jì)算cell高度的庫(kù)FDTemplateLayoutCell。

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 成武县| 九龙坡区| 道真| 九寨沟县| 嫩江县| 和顺县| 巴楚县| 西丰县| 祥云县| 仙桃市| 江城| 余江县| 贵南县| 陈巴尔虎旗| 利川市| 平舆县| 门头沟区| 京山县| 东莞市| 年辖:市辖区| 介休市| 临沭县| 扶绥县| 耒阳市| 漳州市| 咸宁市| 台北市| 正安县| 吉安市| 和田市| 兖州市| 手机| 修武县| 临潭县| 万载县| 朝阳市| 镇宁| 奉新县| 张家川| 横山县| 四川省|