前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >iOS中TableView的不同类型

iOS中TableView的不同类型

作者头像
Cloudox
发布于 2021-11-23 03:37:28
发布于 2021-11-23 03:37:28
1.3K00
代码可运行
举报
文章被收录于专栏:月亮与二进制月亮与二进制
运行总次数:0
代码可运行

TableView是iOS开发中经常用到的View,针对不同的显示需求,我们需要不同的Cell来进行显示,比较复杂的显示我们一般会自定义Cell的样式,但是简单的显示就可以靠iOS本身支持的列表类型了。

iOS目前支持四中列表类型,分别是:

  • UITableViewCellStyleDefault:默认类型,可以显示图片和文本
  • UITableViewCellStyleSubtitle:可以显示图片、文本和子文本
  • UITableViewCellStyleValue1:可以显示图片、文本和子文本
  • UITableViewCellStyleValue2:可以显示文本和子文本

其显示的样式也各不相同,按顺序如下所示:

要设置也很简单,代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    UITableViewCell *cell;  
    // 共四种类型  
    switch (indexPath.row) {  
        case 0:// UITableViewCellStyleDefault:默认的类型,支持显示图片和文本  
        {  
            NSString *CellOne = @"CellOne";  
            // 设置tableview类型  
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellOne];  
            // 设置不可点击  
            cell.selectionStyle = UITableViewCellSelectionStyleNone;  
            cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片  
            cell.textLabel.text = @"textLabel";// 文本  
        }  
            break;  
        case 1:// UITableViewCellStyleSubtitle类型,支持显示图片和文本以及子文本  
        {  
            NSString *CellTwo = @"CellTwo";  
            // 设置tableview类型  
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellTwo];  
            // 设置不可点击  
            cell.selectionStyle = UITableViewCellSelectionStyleNone;  
            cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片  
            cell.textLabel.text = @"textLabel";// 文本  
            cell.detailTextLabel.text = @"detailTextLabel";// 子文本  
        }  
            break;  
        case 2:// UITableViewCellStyleValue1类型,支持显示图片和文本以及子文本  
        {  
            NSString *CellThree = @"CellThree";  
            // 设置tableview类型  
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellThree];  
            // 设置不可点击  
            cell.selectionStyle = UITableViewCellSelectionStyleNone;  
            cell.imageView.image = [UIImage imageNamed:@"icon"];// 图片  
            cell.textLabel.text = @"textLabel";// 文本  
            cell.detailTextLabel.text = @"detailTextLabel";// 子文本  
        }  
            break;  
        case 3:// UITableViewCellStyleValue2类型,支持显示文本以及子文本  
        {  
            NSString *CellFour = @"CellFour";  
            // 设置tableview类型  
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellFour];  
            // 设置不可点击  
            cell.selectionStyle = UITableViewCellSelectionStyleNone;  
            cell.textLabel.text = @"textLabel";// 文本  
            cell.detailTextLabel.text = @"detailTextLabel";// 子文本  
        }  
            break;  
    }  
    return cell;  
}  

可以在我的github获取示例工程:https://github.com/Cloudox/TableTypeDemo

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/11/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验