zoukankan      html  css  js  c++  java
  • iOS 关于UITableView的dequeueReusableCellWithIdentifier

    今天看代码的时候,突然想到了以前的一个问题。

    刚学ios那会儿,常会写出以下代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"registerCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"registerCell"];
        }
        
        [cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]];
    
        return cell;
    }

    这段代码的执行结果就是,屏幕来回滚动几次,cell上不断重复添加子view,内存占用不断提升。

    我当时的解决方法是,去掉dequeueReusableCellWithIdentifier方法,其实就是不用系统的重用功能。这种做法的确可以解决不少问题,但是并不是正确的解决方法。

    这里要先说一下dequeueReusableCellWithIdentifier的作用,这个方法的作用是检索重用pool,看看之中有没有同样Identifier的cell对象,如果有,那么就把这个对象取出来重用。比如,一屏幕有5个cell,第一屏幕载入后,我又继续滚动了1格,那么第一个cell就滚出了屏幕,就进入了重用pool,当我再滚动一格时,系统就可能会把pool中刚放入的那个cell取出来,重用!所以,虽然我可以滚动无数下,但真正的使用的cell对象只有几个。这样做可以节省内存分配和释放的时间。

    根据dequeueReusableCellWithIdentifier的作用我们可以看出,在cellForRowAtIndexPath方法中,不应该含有添加subview之类的操作,cell对象在初始化时就应该包含了所有的view,我们在cellForRowAtIndexPath中仅仅需要操作这些view。

  • 相关阅读:
    转载:揪出MySQL磁盘消耗迅猛的真凶
    转载:MySQL看这一篇就够了
    转载:MySQL:亲测备份策略实例(线上真实备份案例)
    Consul集群搭建 2Server+ 3Client
    consul配置参数大全、详解、总结
    基于consul高可用
    MySQL MGR+ Consul之数据库高可用方案
    MySQL Group Replication-MGR集群
    MySQL binlog_format中sbr 和rbr(Statement-Based and Row-Based Replication)的优缺点
    MySQL binlog2sql-闪回数据
  • 原文地址:https://www.cnblogs.com/breezemist/p/3544871.html
Copyright © 2011-2022 走看看