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;
    }

     

     
  • 相关阅读:
    使用XWAF框架(1)——Web项目的代码分层
    Web框架——XWAF的代码结构和运行机制(4)
    理解Web应用程序的代码结构和运行原理(3)
    获取并安装XWAF框架压缩包(2)
    Angular7教程-06-页面与数据交互
    Angular7教程-05-搭建项目环境
    Angular7教程-04-Angular常用操作(下)
    Angular7教程-03-Angular常用操作(上)
    Angular7教程-02-Angular项目目录及基本文件说明
    Angular7教程-01-Angular开发环境配置
  • 原文地址:https://www.cnblogs.com/luoze/p/5467588.html
Copyright © 2011-2022 走看看