ios之清除cell缓存,解决cell的重用问题。

原文:http://blog.csdn.net/chaoyuan899/article/details/13291637

tableView表格中的cell有重用机制,这是一个很好的东西,可以避免开辟很多的空间内存。但是有时候我们不想让它重用cell,,可以用以下的代码解决。

将这个代码放在:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ }这个函数中执行就好了。

  1. //清楚cell的缓存  
  2. NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];  
  3. for (UIView *subview in subviews) {  
  4.     [subview removeFromSuperview];  
  5. }  



例如:

    1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.       
    4.     static NSString *CellIdentifier = @"Cell";  
    5.       
    6.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    7.       
    8.     if (cell == nil) {  
    9.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
    10.                                        reuseIdentifier: CellIdentifier];  
    11.     }else{  
    12.         //cell中本来就有一个subview,如果是重用cell,则把cell中自己添加的subview清除掉,避免出现重叠问题  
    13.         //         [[cell.subviews objectAtIndex:1] removeFromSuperview];  
    14.         for (UIView *subView in cell.contentView.subviews)  
    15.         {  
    16.             [subView removeFromSuperview];  
    17.         }  
    18.     }  
    19.       
    20.     if (tableView == couponTableView) {  
    21.         //进入优惠券列表  
    22.         cell.textLabel.text = [NSString stringWithFormat:@"%@", [couponArry objectAtIndex:indexPath.row]];  
    23.     }  
    24.     else{  
    25.         //进入团购列表  
    26.         cell.textLabel.text = [NSString stringWithFormat:@"%@", [groupbuyArry objectAtIndex:indexPath.row]];  
    27.     }  
    28.       
    29.     return cell;  
    30. }  
原文地址:https://www.cnblogs.com/whqios/p/4543245.html