zoukankan      html  css  js  c++  java
  • 创建cell的三种方式

    方式一 注册cell -> 无需为cell绑定标识符 [使用UIViewController完成!]

      l  1> static NSString * const ID = @"cell"; // 全局ID变量

      l  2> 在视图加载完成后使用tableView进行注册cell

    - (void)viewDidLoad {

        [super viewDidLoad];

     

        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];

    }

      l  3> 在数据源方法cellForRowAtIndexPath:中直接从缓存池中取cell即可!

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

    // 如果是注册cell.那么下面从tableView缓存池中取cell两种的方式方式都可用!

    // 获取cell 方式一

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 获取cell 方式二

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];  

        cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];

     

        return cell;

    }

    方式二  为cell绑定标识符  -> storyboard中进行设置

      l  1> 在storyboard中为cell绑定标识符

      l  2> 在数据源方法cellForRowAtIndexPath:中直接从缓存池中取cell即可!

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        // 无需判断,直接获取即可!

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

        cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];

        return cell;

    }

      

    方式三  在数据源方法cellForRowAtIndexPath:中直接设置!

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     

        static NSString *ID = @"cell";

     

        // 如果是创建局部变量的ID.那么下面这种从缓存池中取cell的方式会使程序崩溃! // 程序崩溃代码! 错误!

     //  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];

       // 正确程序如下:

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

        }

        cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd",indexPath.row];

        return cell;

    }

  • 相关阅读:
    心得体悟帖---200209(效率太低的事情我是不会做的,这个可以解释我所有的行为)
    心得体悟帖---200209(顺心而动的睡觉方式(6+1或者5+2的睡觉方式))
    按钮用图片做背景hover时闪烁
    范仁义Emmet课程---1、HTML速写之Emmet语法规则
    CSS3疑难问题---5、:before和::before的区别
    reset.css是什么
    日常英语---200209(Abbreviations(Emmet Abbreviations Syntax))
    日常英语---200209(relative(layer model))
    日常英语---200209(Emmet Abbreviations Syntax)
    伪元素和伪类在chrome浏览器里面还是比较好区分的
  • 原文地址:https://www.cnblogs.com/jiayongqiang/p/5327565.html
Copyright © 2011-2022 走看看