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;

    }

  • 相关阅读:
    linux(unix)下.tar.gz解压
    linux 实验室
    Liferea 1.0.15
    编造机中鼠标无法应用标题问题解答
    GnuCash 1.9.8
    KDE 走向跨平台, 支持 Windows 和 Mac OS X
    KDVDCreator:创立视频 VCD、SVCD 和 DVD
    digiKam 0.9.3 颁布公布
    Sysinfo 0.7beta4
    KDE 4.1 特性及公布筹划
  • 原文地址:https://www.cnblogs.com/jiayongqiang/p/5327565.html
Copyright © 2011-2022 走看看