你真的瞭解UITableViewCell重用嗎?

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

- (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展現的原型.不管以前有什麼狀態,所有都要設置一遍.github

UITableView建立同時,會建立一個空的複用池.以後UITableView在內部維護這個複用池.通常狀況下,有兩種用法,一種是在取出一個空的cell的時候再新建一個.一種是預先註冊cell.以後再直接從複用池取出來用,不須要初始化.微信

第一種方法:網絡

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

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

代碼以下:this

#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.spa

第二種方法:代理

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

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

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

使用此方法以後,就不用再判斷取出來的cell是否爲空,由於取出來的cell一定存在.調用dequeueReusableCellWithIdentifier:方法時,會先判斷當前複用池時候有可用複用cell.若是沒有,tableview會在內部幫咱們新建一個cell,其餘的跟方法一同樣orm

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 會有個人微信號,若是有問題你也能夠在那找到我;固然不感興趣無視此信息;

相關文章
相關標籤/搜索