zoukankan      html  css  js  c++  java
  • 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重用的问题了。

     
  • 相关阅读:
    ORACLE的客户端、后台进程
    第一范式、第二范式、第三范式
    在VMware Workstation10下CentOS7虚拟机中创建与主机共享文件夹的详细步骤
    oracle经典查询语句
    vmware 安装XP 32位Professional版本
    增、删、改、查
    Windows/Linux服务器/Git/svn/get和post
    三大原理(计算机原理、操作系统原理、编译原理)两个协议(TCP与HTTP协议)一种结构(数据结构)
    EF删除,查询,Linq查询,Lambda查询,修改链接字符串
    EF添加和修改
  • 原文地址:https://www.cnblogs.com/laolitou-ping/p/13794632.html
Copyright © 2011-2022 走看看