iOS怎么讓tableview支持不同種類的cell

這篇文章主要介紹了iOS怎么讓tableview支持不同種類的cell,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)建站主要從事網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)門頭溝,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108

前言

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

效果圖如下:

iOS怎么讓tableview支持不同種類的cell

準(zhǔn)備工作

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

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

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]);
}

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

先在tableview創(chuàng)建時(shí)注冊需要的不同種類,再判斷index對應(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)識加載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;
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“iOS怎么讓tableview支持不同種類的cell”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)頁名稱:iOS怎么讓tableview支持不同種類的cell
URL鏈接:http://muchs.cn/article26/jopscg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、軟件開發(fā)自適應(yīng)網(wǎng)站、網(wǎng)站排名用戶體驗(yàn)、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)