UITableView基础

UITableView

UITableView的常见设置

// 设置tableView的样式(只读,可在storyBoard中修改)
plain样式下 section的heder和footer会有悬浮效果(通讯录)
group样式 section的头部和底部会默认的有一块间距

// 设置每一行cell的高度
self.tableView.rowHeight = 100;

// 设置每一组头部的高度
self.tableView.sectionHeaderHeight = 50;

// 设置每一组尾部的高度
self.tableView.sectionFooterHeight = 50;

// 设置分割线颜色
self.tableView.separatorColor = [UIColor redColor];

// 设置分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// 设置表头控件 显示在内容的最前面,一开始就可以看见
self.tableView.tableHeaderView = [[UISwitch alloc] init];

// 设置表尾控件 显示在内容的最后面,一开始看不见
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];

// 设置右边索引文字的颜色
self.tableView.sectionIndexColor = [UIColor redColor];

// 设置右边索引文字的背景色
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];

TableView常用属性

tableView.rowHeight 行高
tableView.separatorcolor 分割线颜色
分割线样式
tabbleView.separatorStyle
// UITableViewCellSepartorStyleNone 分割线隐藏
tableView.sectionHeaderHeight 每一组的头部高度
tableView.sectionFooterHerght 每一组的尾部高度

可以在cell底部添加一个view 设置高度为小于1作为全屏分割线或者利用透明度
  • UITableView的索引条是按顺序排列的并不是按对应的索引来排序的
// 返回tableView索引数组
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

UITableView基础

  • 设置dataSource数据源

  • 实现方法

    • 只有一组的时候可以不实现
    • 告诉tableView一共有多少组(默认是0行)
      • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    • 必须实现告诉tableView每组有多少行
      • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    • 必须实现告诉tableView没行显示什么数据(cell)
      • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

可选实现

// 告诉tableView第section组的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
// 告诉tableView第section组的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  • tableView面对模型开发
多组tableView需要的数据
    1.组模型
        包含行模型,上标题(titleForHeader),下标题(titleForFooter)
    2. 行模型 --> 对应cell需要的数据
        图片,主标题,副标题等
单组tableView需要的数据
    1. 行模型 --> 对应cell需要的数据

常用TableView代理方法

// 选中调用cell时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
// 取消点击调用 
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
// 告诉tableView第indexPath行的行高 heightForRow
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
// 修改头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
// 修改底部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
// 设置顶部控件
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
// 设置底部控件
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
// tableView继承自scrollView,tableView的代理同时可以使用scrollView的代理方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值