iOS解決UITableView中Cell重用帶來的問題


tableView的常規配置,當超出一屏的cell就會標上可重用的標識出列到可重用緩存池中,后面再根據可重用標識來到的可重的cell就會和前面顯示同樣內容。

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

    // 定義唯一標識

    static NSString *CellIdentifier = @"Cell";

    // 通過唯一標識創建cell實例

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 判斷為空進行初始化  --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化)

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    // 對cell 進行簡單地數據配置

    cell.textLabel.text = @"text";

         cell.detailTextLabel.text = @"text";

    cell.imageView.image = [UIImage imageNamed:@"4.png"];

    return cell;

}

 

 

方法一: 取消cell的重用機制,通過indexPath來創建cell 將可以解決重復顯示問題 不過這樣做相對於大數據來說內存就比較吃緊了,通過不讓它重用cell,來解決重復顯示

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

    // 定義唯一標識

    static NSString *CellIdentifier = @"Cell";

    // 通過indexPath創建cell實例 每一個cell都是單獨的

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // 判斷為空進行初始化  --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化)

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    // 對cell 進行簡單地數據配置

    cell.textLabel.text = @"text";

    cell.detailTextLabel.text = @"text";

    cell.imageView.image = [UIImage imageNamed:@"4.png"];

    return cell;

}

 

 

方法二: 讓每個cell都擁有一個對應的標識 這樣做也會讓cell無法重用 所以也就不會是重復顯示了 顯示內容比較多時內存占用也是比較多的和方案一類似,同樣通過不讓它重用cell來解決重復顯示,不同的每個cell對應一個重用標識

 

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

    

    // 定義cell標識  每個cell對應一個自己的標識

    

    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];

    

    // 通過不同標識創建cell實例

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

    // 判斷為空進行初始化  --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化)

    

    if (!cell) {

        

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        

    }

    

    // 對cell 進行簡單地數據配置

    

    cell.textLabel.text = @"text";

    

    cell.detailTextLabel.text = @"text";

    

    cell.imageView.image = [UIImage imageNamed:@"4.png"];

    

    return cell;

}

 

 

方法三: 只要最后一個顯示的cell內容不為空,然后把它的子視圖全部刪除,等同於把這個cell單獨出來了 然后跟新數據就可以解決重復顯示,同樣通過不讓他重用cell來解決重復顯示,不同的每個cell對應一個標識。

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

    // 定義唯一標識

    static NSString *CellIdentifier = @"Cell";

    // 通過唯一標識創建cell實例

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 判斷為空進行初始化  --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化)

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }else{

        //當頁面拉動的時候 當cell存在並且最后一個存在 把它進行刪除就出來一個獨特的cell我們在進行數據配置即可避免

        while ([cell.contentView.subviews lastObject] != nil) {

            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];

        }

    }

    // 對cell 進行簡單地數據配置

    cell.textLabel.text = @"text";

    cell.detailTextLabel.text = @"text";

    cell.imageView.image = [UIImage imageNamed:@"4.png"];    

    return cell;

}

 


免責聲明!

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



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