這篇文章主要介紹了iOS怎么讓tableview支持不同種類的cell,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)主營西烏珠穆沁網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,重慶APP開發(fā)公司,西烏珠穆沁h5微信小程序開發(fā)搭建,西烏珠穆沁網(wǎng)站營銷推廣歡迎西烏珠穆沁等地區(qū)企業(yè)咨詢前言
我們在項目中偶爾需要讓tableview里支持不同種類的cell,比如微博的原創(chuàng)微博和別人轉(zhuǎn)發(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; }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“iOS怎么讓tableview支持不同種類的cell”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)建站,關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道,更多相關知識等著你來學習!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁名稱:iOS怎么讓tableview支持不同種類的cell-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://muchs.cn/article30/cdopso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、小程序開發(fā)、商城網(wǎng)站、定制網(wǎng)站、外貿(mào)建站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容