zoukankan      html  css  js  c++  java
  • UITableView-cell的循环利用

    cell的循环利用方式1

    /**
     *  什么时候调用:每当有一个cell进入视野范围内就会调用
     */
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 0.重用标识
        // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
        static NSString *ID = @"cell";
    
        // 1.先根据cell的标识去缓存池中查找可循环利用的cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 2.如果cell为nil(缓存池找不到对应的cell)
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
    
        // 3.覆盖数据
        cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    
        return cell;
    }

    cell的循环利用方式2

    • 定义一个全局变量
    // 定义重用标识
    NSString *ID = @"cell";
    • 注册某个标识对应的cell类型
    // 在这个方法中注册cell
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 注册某个标识对应的cell类型
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    }
    • 在数据源方法中返回cell
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 1.去缓存池中查找cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 2.覆盖数据
        cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    
        return cell;
    }
     

    cell的循环利用方式3

    • 在storyboard中设置UITableView的Dynamic Prototypes Cell 

    • 设置cell的重用标识 

    • 在代码中利用重用标识获取cell

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 0.重用标识
        // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
        static NSString *ID = @"cell";
    
        // 1.先根据cell的标识去缓存池中查找可循环利用的cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 2.覆盖数据
        cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    
        return cell;
    }

     

     
  • 相关阅读:
    linux下C语言socket网络编程简例
    cJSON学习笔记 续集
    用javac编译整个j2ee项目
    如何用javac 和java 编译运行整个Java工程
    Log4j 日志级别
    (获取选中的光标起始位置)EditText常用属性【三】:EditText选取操作
    Linux下启动和停止Java应用程序的Shell脚本
    (判断url文件大小)关于inputStream.available()方法获取下载文件的总大小
    java 从网络Url中下载文件
    JavaMail入门:创建纯文本、HTML格式的邮件
  • 原文地址:https://www.cnblogs.com/luoze/p/5467588.html
Copyright © 2011-2022 走看看