首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS开发-UITableViewCell的创建方式

iOS开发-UITableViewCell的创建方式

作者头像
孙寅
发布2020-06-02 18:28:43
1.2K0
发布2020-06-02 18:28:43
举报
文章被收录于专栏:宜达数字宜达数字宜达数字

这几天被这个基础控件搞的头疼

第一种方法: 纯手码(最土的方法)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ID = @"cell";
    // 根据标识去缓存池找cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  // 不写这句直接崩掉,找不到循环引用的cell  
   if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
   }   
    cell.textLabel.text = @"123";
    return cell;
}
第二种方法: 结合Storyboard
1、让tableViewController控制器的类型是我自己定义的SYTableController ->继承UITableViewConroller

Paste_Image.png

Paste_Image.png

Paste_Image.png

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

    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    return cell;
}
 /*   indexPath 里面有两个参数
         *   一个是cell的组    indexPath.section
         *
         *   另一个是cell的行   indexPath.row
         */

Paste_Image.png

小Model演示不同的cell(同一个Storyboard)
1、先在Storyboard建立2个cell

Paste_Image.png

2、给cell绑定cell1/cell2标识

Paste_Image.png

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.row % 2 == 0) { // 是偶数行加载----cell1

            static NSString *ID = @"cell1";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            
            return cell;
            
        }else  // 奇数行加载----cell2
        {
            static NSString *ID = @"cell2";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            
            return cell;
        }
}
1、封装一下,tableView返回cell的方法中就没有这么多代码了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.row % 2 == 0) { // 如果是偶数行加载----cell1

            return [self cell1:tableView cellForRowAtIndexPath:indexPath];
        }else  // 如果是奇数行加载----cell2
        {
            return [self cell2:tableView cellForRowAtIndexPath:indexPath];
        }
}

- (UITableViewCell *)cell1:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath{

    static NSString *ID = @"cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    return cell;
}

- (UITableViewCell *)cell2:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath{
    static NSString *ID = @"cell2";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    return cell;
}
注意:实际开发中自定义cell肯定是不能这样封装的,因为数据不一样的
cell补充:

cell

Paste_Image.png

custorm:自定义,可以随意往里面拖一些东西

被static修饰的成员变量,全局只有一份内存 static的后面只可以放一个定值,这是在编译阶段就确定的

   错误: static NSString *ID = indexPath.row % 2 == 0 ?@"cell1" : @"cell2";
   正确: static NSString *ID = @"cell1";

cell里面其实有一个contentView,cell里面的控件都在这个里面

Paste_Image.png

- (UITableViewCell *)cell1WithTable:(UITableView *)tableView
{
    static NSString *ID = @"cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 苹果建议,如果给cell里面添加控件,直接调用cell.contentView
    [cell.contentView addSubview:nil];
    // 虽然我们可以在cell直接点出cell里面的控件,
    // 其实这些点出来的控件的父控件都是cell.contentView
    cell.imageView.superview = cell.contentView
    
    return cell;
}

使用ViewController往里面添加一个tableView也是可以的

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一种方法: 纯手码(最土的方法)
  • 第二种方法: 结合Storyboard
    • 1、让tableViewController控制器的类型是我自己定义的SYTableController ->继承UITableViewConroller
    • 小Model演示不同的cell(同一个Storyboard)
      • 1、先在Storyboard建立2个cell
        • 2、给cell绑定cell1/cell2标识
        • 1、封装一下,tableView返回cell的方法中就没有这么多代码了
          • cell补充:
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档