UIKit框架-10.UITableView概述

1.UITableView概述

  • 在众多移动应用中,我们能够看到各式各样的列表数据,例如QQ消息,微博,网易新闻,微信朋友圈等等,都是列表数据,并且可以滚动
    这里写图片描述
    这里写图片描述
  • 在IOS中,苹果为我们提供了UITableView控件,专门用来展示列表数据,UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳,所以在IOS开发中我们一般使用TableView来展示列表数据

2.UITableView基本属性介绍

  • 初始化
- (void)viewDidLoad {
   // 1.初始化tableView,tableview有两种样式,一种是单一样式UITableViewStylePlain,一种是分组样式UITableViewStyleGrouped
// 1.1 @property (nonatomic, readonly) UITableViewStyle style;是只读属性,所以只能在初始化时指定样式
    UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //添加控件到视图
    [self.view addSubview:tv];
}
  • tableView要想展示数据,必须设置数据源,这里我们设置数据源为控制器,还必须实现数据源的三个方法
//在viewDidLoad方法中设置数据源为控制器
- (void)viewDidLoad {
    tv.dataSource = self;
}
#pragma mark - UITableViewDataSource
//方法一:返回有多少组数据
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
// 返回每组有多少行数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (0 == section) {
        return 3;
    }else
        return 5;
}
// 返回每行显示什么内容(每一个cell就是一行单元格数据行)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    1.创建cell
        UITableViewCell *ce11 = [[UITableViewCell alloc] init];
    //    2.设置cell的数据
    cell.textLabel.text = [NSString stringWithFormat:@"我是数据%zd组,%zd行",indexPath.section,indexPath.row];
    //    3.返回cell
    return cell;
}
  • tableView的其他属性设置
  • 行高设置
// 1.修改tableView的行高(默认为44)
    tv.rowHeight = 80;
// 也可以通过代理方法设置(实现代理方法后行高会以代理放回值为准)
// 设置每组每行的高度
-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
}
  • 组头组尾设置
// 2.修改组头和组尾的高
    tv.sectionHeaderHeight = 100;
    tv.sectionFooterHeight = 33;
// 2.1要想设置组头和组尾的高度,需要实现显示组头内容和组尾内容的代理方法
//设置组头标题
-(NSString *)tableView:(nonnull UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"表头%zd",section];
}
//设置组尾标题
-(NSString *)tableView:(nonnull UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"表尾%zd",section];
}
//2.2或者实现设置组头和组尾视图的代理方法
// 设置组头视图
-(UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIButton buttonWithType:UIButtonTypeContactAdd];
}
// 设置组尾视图
-(UIView *)tableView:(nonnull UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIButton buttonWithType:UIButtonTypeInfoLight];
}
// 取出对应组的头部或尾部视图,对象方法
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section;
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section;
  • 整个table的头部和尾部视图设置
// 3.设置整个tableView的头部和尾部视图(不要和footViewforSection混淆)
    tv.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    tv.tableHeaderView = [[UISwitch alloc] init];
  • 分割线设置
// 4.设置分割线属性
// 4.1 设置分割线颜色
    tv.separatorColor = [UIColor redColor];
// 4.2 设置分割线样式
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
    UITableViewCellSeparatorStyleNone,// 没有分割线
    UITableViewCellSeparatorStyleSingleLine,// 单行线
    UITableViewCellSeparatorStyleSingleLineEtched   // 只支持分组样式
};
    tv.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  • 设置索引条
// 5.设置索引条属性
// 5.1设置索引条颜色
    tv.sectionIndexColor = [UIColor orangeColor];
// 5.2设置索引条背景颜色
    tv.sectionIndexBackgroundColor = [UIColor greenColor];
// 5.3设置索引条的数据源方法
// 需要返回字符串数组
-(nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(nonnull UITableView *)tableView{
    return @[@"A",@"B",@"C",@"D"];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值