前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITableView基础1. UITableView的作用2. UITableView的常用属性3. tableView展示数据三部曲4. UITableViewStyleGrouped样式5. U

UITableView基础1. UITableView的作用2. UITableView的常用属性3. tableView展示数据三部曲4. UITableViewStyleGrouped样式5. U

作者头像
stanbai
发布2018-06-28 13:18:37
7720
发布2018-06-28 13:18:37
举报

1. UITableView的作用

  • 以垂直滚动方式显示数据列表。
  • UITableView 的两种内置样式:UITableViewStylePlain 和UITableViewStyleGrouped 。
  • tableView只是一个愚蠢的容器,只负责显示。显示的是什么自己完全不知道。
  • 数据都是由dataSource提供。 要满足快速滚动,性能很重要

2. UITableView的常用属性

2.1 分割线属性

属性名称

作用

separatorStyle

分隔线样式

separatorColor

分隔线颜色

2.2 选中属性

属性名称

作用

allowsSelection

允许选中

allowsMultipleSelection

允许多选

2.3 行数

属性名称

作用

indexPathsForSelectedRows

当前选中行数

indexPathsForVisibleRows

当前可见行数

2.4 背景

属性名称

作用

backgroundView

背景视图

selectedBackgroundView

选中时的背景视图

2.5 UITableViewCell的selectionStyle属性可设置被选中时的背景颜色

属性名称

作用

UITableViewCellSelectionStyleNone

没有颜色

UITableViewCellSelectionStyleBlue

蓝色(默认)

UITableViewCellSelectionStyleGray

灰色

3. tableView展示数据三部曲

  1. 遵守数据源协议;
  2. 设置数据源
  3. 实现相应数据源方法
  • cell的默认高度是44,宽度和tableView等宽。

3.1 遵守数据源

@interface ViewController ()<UITableViewDataSource>

3.2 设置数据源

    self.tableView.dataSource = self;

3.3 实现数据源方法

  1. 总共多少组
  2. 每组多少行
  3. 每组中每行的内容
//返回有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//返回有多少行,section 组的索引
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

//返回每一组的每一行显示什么内容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建UITableViewCell
    UITableViewCell *tableViewCell = [[UITableViewCell alloc]init];
    
    return tableViewCell;
}

4. UITableViewStyleGrouped样式

4.1 使用storyboard设置

Paste_Image.png

4.2 使用代码设置

Paste_Image.png

  • UITableViewStyle的style的属性是一个只读属性,所以修改不了。
  • 默认创建出来就是UITableViewStylePlain。
  • 但是可以在初始化的时候直接定义。
    UITableView *haha = [UITableView alloc]initWithFrame:<#(CGRect)#> style:(UITableViewStyle)];

5. UITableViewCell

5.1 四种默认样式

Paste_Image.png

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
    UITableViewCellStyleValue1,     // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
    UITableViewCellStyleValue2,     // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
    UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
};             // available in iPhone OS 3.0

满足不了需求,就需要自定义样式。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. UITableView的作用
  • 2. UITableView的常用属性
    • 2.1 分割线属性
      • 2.2 选中属性
        • 2.3 行数
          • 2.4 背景
            • 2.5 UITableViewCell的selectionStyle属性可设置被选中时的背景颜色
            • 3. tableView展示数据三部曲
              • 3.1 遵守数据源
                • 3.2 设置数据源
                  • 3.3 实现数据源方法
                  • 4. UITableViewStyleGrouped样式
                    • 4.1 使用storyboard设置
                      • 4.2 使用代码设置
                      • 5. UITableViewCell
                        • 5.1 四种默认样式
                        相关产品与服务
                        容器服务
                        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档