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的代理方法