1. 程式人生 > >iOS UITableView中的cell因為重用機制導致新的cell的資料出現重複或者錯亂

iOS UITableView中的cell因為重用機制導致新的cell的資料出現重複或者錯亂

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

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

解決方法:

方法1

將獲得cell的方法從- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 換為-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

重用機制呼叫的就是dequeueReusableCellWithIdentifier這個方法,方法的意思就是“出列可重用的cell”,因而只要將它換為cellForRowAtIndexPath(只從要更新的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];
    }
     //...其他程式碼                              
}</span>

方法2 

通過為每個cell指定不同的重用識別符號(reuseIdentifier)來解決。       重用機制是根據相同的識別符號來重用cell的,識別符號不同的cell不能彼此重用。於是我們將每個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];
    }
    //...其他程式碼
}


方法3

刪除重用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];
        }
    }
    //...其他程式碼
}