重用cell的兩種方法

來源:互聯網
上載者:User

標籤:

今天在學習IAP的時候無意間看到原來  tableView: cellForRowAtIndexPath:方法中有兩個獲得重用cell的方法 ,一直以來都是用UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];那下面的這個怎麼用呢,感覺比較怪,假設沒有重用的豈不是為空白了UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]具體有什麼區別呢,並且當我用  UITableViewCell  *cell  =  [tableView  dequeueReusableCellWithI dentifier:CellIdentifier  forIndexPath:indexPath]的時候,為什麼總報錯 
reason:  ‘unable  to  dequeue  a  cell  with  identifier  Cell  -  must  register  a  nib  or  a  class  for  the  identifier  or  connect  a  prototype  cell  in  a  storyboard‘,經過查閱,知道原來此方法為6.0新加的,在SDK5.0是運行不起來的。 如果需要使用這個方法,你必須使用配套的方法來一起用,下面兩個配套方法選其一:
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0); 
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

3  比如你已經用NIB做了一個Cell,或者自訂了一個Cell。我們在你建立UITableView的時候,就可以順帶 
self.tableView.backgroundColor = xxxx; 
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];

這樣你在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath這個方法裡,你就可以省下這些代碼: 
    static NSString *CellIdentifier = @"Cell"; 
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

 if (cell == nil) {         
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier];       
  //設定你的cell 

而只需要 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
//設定你的cell 
整體來說6.0卻是比5.0更省勁 

重用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.