你真的了解UITableViewCell重用嗎?


一:首先查看一下關於UITableViewCell重用的定義

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  

- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

在tableview新建的時候,會新建一個復用池(reuse pool).這個復用池可能是一個隊列,或者是一個鏈表,保存着當前的Cell.pool中的對象的復用標識符就是reuseIdentifier,標識着不同的種類的cell.所以調用dequeueReusableCellWithIdentifier:方法獲取cell.從pool中取出來的cell都是tableview展示的原型.無論之前有什么狀態,全部都要設置一遍.

UITableView創建同時,會創建一個空的復用池.之后UITableView在內部維護這個復用池.一般情況下,有兩種用法,一種是在取出一個空的cell的時候再新建一個.一種是預先注冊cell.之后再直接從復用池取出來用,不需要初始化.

第一種方法:

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  

UITableview第一次執行tableView:cellForRowAtIndexPath:.當前復用池為空.dequeueReusableCellWithIdentifier調用中取出cell,並檢測cell是否存在.目前並不存在任何cell,於是新建一個cell,執行初始化, 並return cell.

代碼如下:

#define kInputCellReuseIdentifierPWD   @"password_cell"
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kInputCellReuseIdentifierPWD];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kInputCellReuseIdentifierPWD];
}

cell.textLabel.text = @"It is awesome";
return cell;

上面的代碼中,你返回的cell會被UITableView添加到復用池中.第二次調用tableView:cellForRowAtIndexPath:,當前復用池中有一個cell.這時候因為UITableView上面還未填滿,而且復用池中唯一的那一個已經在使用了.所以取出來的Cell仍然是nil.於是繼續新建一個cell並返回,復用池再添加一個cell,當前復用池中cell的個數為2.假如當前tableview只能容納5個cell.那么在滾動到第6個cell時,從tableview的復用池取出來的cell將會是第0行的那個cell.以此類推,當滾動到第7行時,會從復用池取出來第1行的那個cell. 另外,此時不再繼續往復用池添加新的cell.

第二種方法:

- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

在UITableView初始化的時候,注冊一個UITableViewCell的類;不用再做空判斷;

[tableView registerClass:[BLSProjectMoneyCompleteCell class] forCellWithReuseIdentifier:NSStringFromClass([BLSProjectMoneyCompleteCell class])];

使用此方法之后,就不用再判斷取出來的cell是否為空,因為取出來的cell必定存在.調用dequeueReusableCellWithIdentifier:方法時,會先判斷當前復用池時候有可用復用cell.如果沒有,tableview會在內部幫我們新建一個cell,其他的跟方法一一樣

BLSProjectMoneyCompleteCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BLSProjectMoneyCompleteCell class])];

二:UITableViewCell重用時引發的問題

UITableView中的cell可以有很多,一般會通過重用cell來達到節省內存的目的:通過為每個cell指定一個重用標識符(reuseIdentifier),即指定了單元格的種類,當cell滾出屏幕時,會將滾出屏幕的單元格放入重用的queue中,當某個未在屏幕上的單元格要顯示的時候,就從這個queue中取出單元格進行重用。

但對於多變的自定義cell,有時這種重用機制會出錯。比如,當一個cell含有一個UITextField的子類並被放在重用queue中以待重用,這時如果一個未包含任何子視圖的cell要顯示在屏幕上,就會取出並使用這個重用的cell顯示在無任何子視圖的cell中,這時候就會出錯。

方法一:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改為以下的方法 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根據indexPath准確地取出一行,而不是從cell重用隊列中取出 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
     //...其他代碼                               
} 

將獲得cell的方法從- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 換為-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;重用機制調用的就是dequeueReusableCellWithIdentifier這個方法,方法的意思就是“出列可重用的cell”,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell),就可以不使用重用機制,因而問題就可以得到解決,雖然可能會浪費一些空間。

方法二:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath來唯一確定cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    //...其他代碼 
} 

通過為每個cell指定不同的重用標識符(reuseIdentifier)來解決。重用機制是根據相同的標識符來重用cell的,標識符不同的cell不能彼此重用。於是我們將每個cell的標識符都設置為不同,就可以避免不同cell重用的問題了。

方法三:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    else 
    { 
        //刪除cell的所有子視圖 
        while ([cell.contentView.subviews lastObject] != nil) 
        { 
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; 
        } 
    } 
    //...其他代碼 
}

刪除重用cell的所有子視圖;這個方法是通過刪除重用的cell的所有子視圖,從而得到一個沒有特殊格式的cell,供其他cell重用。考慮到內存問題,cell少得時候可以每個都添加標識符,當cell重用較多時,考慮內存問題,建議用刪除cell的所有子視圖方法(做視頻播放的時候).

三: 有時Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath: 閃退問題

在先前做的分組列表中,兩組的Cell是不一樣樣式,兩組分別運用如下的代碼(也有在初始化注冊Cell),在IOS9以下會閃退

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                     forIndexPath:indexPath];

后來修改成:

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell==nil) {
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

 四:prepareForReuse方法去重的效果

cell被重用如何提前知道? 重寫cell的prepareForReuse官方頭文件中有說明.當前已經被分配的cell如果被重用了(通常是滾動出屏幕外了),會調用cell的prepareForReuse通知cell.注意這里重寫方法的時候,注意一定要調用父類方法[super prepareForReuse] .這個在使用cell作為網絡訪問的代理容器時尤其要注意,需要在這里通知取消掉前一次網絡請求.不要再給這個cell發數據了.
// if the cell is reusable (has a reuse identifier), this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:. If you override, you MUST call super.

- (void)prepareForReuse
{
[super prepareForReuse];
}

自定義UITableViewCell的方法有很多 發現一些人都會遇到自己定義的cell里面圖片錯亂的問題 這個問題往往是因為沒有實現prepareForReuse這個方法導致的.

UITableViewCell在向下滾動時復用, 得用的cell就是滑出去的那些, 而滑出去的cell里顯示的信息就在這里出現了 解決的方法就是在UITableViewCell的子類里實現perpareForReuse方法, 把內容清空掉,就可以對控件進行清空,實現被重復增加的問題;

-(void)prepareForReuse
{
   [super prepareForResuse];

    _titleLb.text = nil;
    _commentLabel.text = nil;
    _standarLabel.text = nil;
    _photos = nil;

    _line1.hidden = YES;
    _line2.hidden = YES;
    for (UIView *view in _imgViews.subviews) {
        if (view.tag >0 && ([view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UILabel class]])) {
            [view removeFromSuperview];
        }
    }
}

 

 

最近有個妹子弄的一個關於擴大眼界跟內含的訂閱號,每天都會更新一些深度內容,在這里如果你感興趣也可以關注一下(嘿對美女跟知識感興趣),當然可以關注后輸入:github 會有我的微信號,如果有問題你也可以在那找到我;當然不感興趣無視此信息;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM