iOS如何讓tableview支持不同種類的cell詳解
前言
我們在項目中偶爾需要讓tableview里支持不同種類的cell,比如微博的原創(chuàng)微博和別人轉發(fā)的微博,就是兩種cell。又或是類似支付寶的的timeline也有各種類型的cell。在同一個tableview里實現(xiàn)不同種類的cell,一般有兩種方法,一種是把所有種類的cell先注冊了,再根據(jù)不同的identifer去加載cell,一種是在init時創(chuàng)建不同的identifer的cell。
效果圖如下:
準備工作
創(chuàng)建一個基類的CDZBaseCell,基類cell擁有一些共用的屬性和方法,如持有model,解析model。
創(chuàng)建不同的子類cell,以兩個子類CDZTypeACell CDZTypeBCell 為例,繼承自CDZBaseCell,重寫一些方法,如高度,顯示視圖等等。
Datasource中準備好判斷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)建時注冊需要的不同種類,再判斷index對應的種類,再根據(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ù)重用標識加載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時創(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; }
源碼下載
總結
個人更喜歡第二種,蘋果官方文檔也推薦第二種方法去重用cell。我覺得優(yōu)點是一個是在tableview劃分MVC架構時,tableview創(chuàng)建時不需要知道cell的類型,而只需要知道datasouce,而datasource才是需要去分配cell類型的。第二個是tableviewcell的初始化方法并非只能用initWithStyle(collectionview必須先注冊的原因則在于初始化方法只有initWithFrame)。而使用了注冊,則是在復用池空時默認調用initWithStyle的方法,如果需要用別的方法初始化就不可以了。第一種方法可以用在有一些庫需要先注冊后才能調用的,比如自動計算cell高度的庫FDTemplateLayoutCell。
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
IOS 中UITextField和UITextView中字符串為空和空格的解決辦法
這篇文章主要介紹了IOS 中UITextField和UITextView中字符串為空和空格的解決辦法的相關資料,需要的朋友可以參考下2017-07-07iOS使用音頻處理框架The Amazing Audio Engine實現(xiàn)音頻錄制播放
這篇文章主要為大家詳細介紹了iOS使用音頻處理框架The Amazing Audio Engine實現(xiàn)音頻錄制播放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04