• TableView 注册问题


    1.自定义cell时,
    若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
    不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
    2.需不需要注册?
    使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;
    使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。

    - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; 
     // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
    (返回给代理一个已经分配的cell,代替一个新的cell,如果没有已分配的cell,则返回nil,使用这个方法就不需要注册了)
    
    - (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 
    // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered  
    如果cell的identifier是注册过的,那么这个新列出的方法保证返回一个cell (有分配的就返回已分配的cell,没有返回新的cell)并适当调整大小,
    可省略cell空值判断步骤,用这个方法cell必须注册,不是自定义的cell,UITableViewCell也要注册

    重用有问题
      方法 ①:
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

                             改为以下的方法:
                                  UITableViewCell *cell = [tableViewcellForRowAtIndexPath:indexPath];

                                               注 :     重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,

                                                                               因而只要将它换为cellForRowAtIndexPath,就可以不使用重用机制, 

                                                                               因而问题就可以得到解决,虽然可能会浪费一些空间。

    方法 ②:

                           通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决

     

                         重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。

                              于是我们将每个cell的标识符都设置为不同,就可以避免不同cell重用的问题了。

     
  • 相关阅读:
    图像处理之优化---任意半径局部直方图类算法在PC中快速实现的框架
    新的验证方式---短信验证和语言验证
    习武 之路---通背拳和苗刀!
    模式识别之Shape Context---利用Shape Context进行形状识别
    ELK 部署
    rsync实现文件备份同步
    oracle-3-子查询和常用函数
    oracle-2中commit 详解
    使用nginx绑定域名,代理gitlab
    Linux Crontab 安装使用详细说明
  • 原文地址:https://www.cnblogs.com/laolitou-ping/p/13794632.html
Copyright © 2020-2023  润新知