zoukankan      html  css  js  c++  java
  • 52.tableViewCell重用机制避免重复显示问题

    表刷新超出页面显示的内容会重复出现

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

        //定义唯一标识

        static NSString *cellId = @"Cell";

        //通过唯一标识创建cell实例

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

        //判断为空进行初始化 -- (刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

        if(!cell){

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

        }

        cell.textLabel.text = @"你好!欢迎你";

        return  cell;

    }

    以下三个方法解决:

    1.取消cell的重用机制,通过indexPath来创建cell将可以解决重复出现问题,不过这样做对于大数据来说内存就比较紧张

      

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

        //定义唯一标识

        static NSString *cellId = @"Cell";

        //通过indexPath创建cell实例,每个cell都是单独的

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        //判断为空进行初始化(刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

        if(!cell){

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

        }

        cell.textLabel.text = @"你好!欢迎你";

        return  cell;

    }

    2.让每个cell都拥有一个对应的标识 这样做也会让 cell无法重用 所以也就不会重复出现啦  显示内容比较多时内存占用也是比较多的和方法一类似

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

        //定义cell 标识  每个cell对应一个自己的标识

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

        //通过不同标识创建cell实例

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        //判断为空进行初始化(刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

        if(!cell){

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

        }

        cell.textLabel.text = @"你好!欢迎你";

        return  cell;

    }

    3. 只要最后一个显示的cell内容不为空,然后把它的子视图全部删除,等同于把这个cell单独出来了,然后刷新数据就可以解决重复显示问题

        刷新需要显示新数据的时候,把最后一个cell进行删除,就有可以自定义cell,此方案既可避免重复出现,又重用了cell相对内存管理来说最好的方案,前两者相比比较消耗内存

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

        //定义唯一标识

        static NSString *cellId = @"Cell";

        //通过唯一标识创建cell实例

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

        //判断为空进行初始化(刷动页面超出屏幕的时候就会重用之前的cell,而不会再次初始化)

        if(!cell){

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

        }

        else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们再进行数据配置即可避免

                 {

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

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

                            }

                    }

        cell.textLabel.text = @"你好!欢迎你";

        return  cell;

    }

    *******************多点了解*************************8

    - (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
         static  NSString  *CellIdentiferId = @"MomentsViewControllerCellID";
         UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
         if (cell == nil) {
            NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MomentsCell" owner:nil options:nil];
            cell = [nibs lastObject];
            cell.backgroundColor = [UIColor clearColor];
            

          };
            
         }
         return cell;

    }
    严重注意:我们之前这么用都没注意过重用的问题,这样写,如果在xib页面没有设置 重用字符串的话,是不能够被重用的,也就是每次都会重新创建,这是严重浪费内存的
  • 相关阅读:
    HBase
    linux配置环境变量
    ubuntu17.04安装flash
    WebService服务及客户端 编程
    eclipse
    设计模式:简单工厂
    设计模式:工厂方法
    C#加载dll 创建类对象
    C#线程
    Opencv 3入门(毛星云)摘要
  • 原文地址:https://www.cnblogs.com/qiangzheVSruozhe/p/9274714.html
Copyright © 2011-2022 走看看