前言
我們?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,重寫(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;}源碼下載
總結(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)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注