最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年08月10日 10:07:28   作者:Nemocdz  
這篇文章主要給大家介紹了關(guān)于iOS如何讓tableview支持不同種類的cell的相關(guān)資料,文中通過示例代碼詳細(xì)的給大家介紹了實(shí)現(xiàn)的兩種方法,對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

我們?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。

效果圖如下:


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

方法一:先注冊(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è)人更喜歡第二種,蘋果官方文檔也推薦第二種方法去重用cell。我覺得優(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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

澳门| 丹凤县| 建德市| 新绛县| 洛浦县| 塔河县| 高州市| 昭通市| 新营市| 比如县| 四川省| 韶关市| 资中县| 攀枝花市| 上栗县| 茶陵县| 夏津县| 绍兴县| 玉环县| 米泉市| 凭祥市| 赣榆县| 水城县| 玛纳斯县| 勐海县| 嘉兴市| 拉孜县| 巴林右旗| 漾濞| 穆棱市| 改则县| 衡水市| 安岳县| 梅河口市| 华坪县| 甘泉县| 高安市| 马公市| 二手房| 南涧| 开江县|