iOS學習——UITableViewCell兩種重用方法的區別,iosuitableviewcell

來源:互聯網
上載者:User

iOS學習——UITableViewCell兩種重用方法的區別,iosuitableviewcell

  今天在開發過程中用到了UITableView,在對cell進行設定的時候,我發現對UITableViewCell的重用設定的方法有如下兩種,剛開始我也不太清楚這兩種之間有什麼區別。直到我在使用方法二進行重用的時候,具體實現代碼如下,其中CJMeetingReplyBasicCell是我自訂的UITableViewCell類型,但是在啟動並執行時候每次都在調用 CJMeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath]; 時崩潰,通過尋找各種原因,確定不是自己代碼的問題之後,開始瞭解這兩種重用方法的區別。那麼,這兩種重用UITableViewCell的方法之間到底有什麼區別呢?

//方法一UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//方法二UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];    if (!cell) {        cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"];    }        return cell;}
一 官網文檔解釋

  首先我們看一下在iOS源碼的UITableView.h中對兩者的解釋如下,我們可以看到方法二是在iOS 6.0中開始推出的新方法,在對方法二的解釋中,我們注意標紅的部分的意思是假設我們已經註冊了標識符,這裡我們猜測可能是我們需要對標識符進行註冊。

//UITableView.h- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

  接下來我們找到官方文檔,https://developer.apple.com/documentation/uikit/uitableview/1614878-dequeuereusablecellwithidentifie?language=objc,官方文檔對方法二的解釋有兩點需要注意,第一個是傳回值的地方如,這裡說道該方法總是返回一個有效UITableViewCell,這是與方法一不同的地方之一。

  第二個需要注意的地方是,在該頁面下面有一個Important的提示如,該提示就說明了方法二的正確使用方法。這裡解釋說要先進行註冊我們自訂或者通過nib的類和標識符,然後再使用方法二進行重用。所以現在我們崩潰的原因就已經明確了,問題就出在沒有進行先註冊我們自訂的類和標識符。

  對於這兩種方法的常規使用方法,下面進行總結一下。

  首先,對於方法一,使用方法很簡單,無需進行其他的定義和註冊,代碼如下。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell"];    if (!cell) {        cell = [[MeetingReplyBasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BasicCell"];    }        return cell;}

  接下來,我們簡單使用方法二進行重用,具體步驟代碼如下。

//首先,我們再tableview進行配置的時候需要註冊我們已定義的cell類和重用標識符self.tableView.backgroundColor = xxxx;[self.tableView registerClass:[MeetingReplyBasicCell class] forCellReuseIdentifier:@"BasicCell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    MeetingReplyBasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath];        return cell;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.