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;

    }

  • 相关阅读:
    Enum.GetUnderlyingType(obj.GetType())
    Out,ref,params修饰符,可选参数,命名参数
    Linq
    var
    checked,unchecked
    StringBuilder.sb.AppendLine();
    js改变css样式的三种方法
    flex的用途
    clip-path
    json 对象 数组
  • 原文地址:https://www.cnblogs.com/jiayongqiang/p/5327565.html
Copyright © 2011-2022 走看看