zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    ubuntu16.04服务自启动(弹控制台)
    第十集 爱在天路唐古拉,青藏梦止长江源
    第九集 生死穿越风火山,感受尘世间轮回
    第八集 昆仑初度尘未洗,夜宿禁区五道梁
    第七集 久历风尘凡间路,终见西域玉珠峰
    第六集 飞沙走石共患难,夜扎昆仑饮圣泉
    第五集 插肩而过茶卡湖,朝发夕至格尔木
    辞职之后在家的挣扎与老爸的工作
    2018,怎么这么难,我该怎么办,我的ai和感情
    17年11月兄弟小聚
  • 原文地址:https://www.cnblogs.com/whqios/p/4543245.html
Copyright © 2011-2022 走看看