UITableView
2016-08-31 本文已影响0人
vip4iPhonr
tableView展示数据
- 设置数据源对象(一般是控制器)
self.tableView.dataSource = self
- 数据源对象需要遵守协议->UITableViewDataSource
@interface ViewController () <UITableViewDatatSource>
@end
-
实现数据源协议里面的方法
- 告诉tableView⼀一共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- 告诉tableView第section组有多少⾏
- (NSInteger)tableView:(UITableViw *)tableView numberOfRowsInSection:(NSInteger)section;
- 告诉tableView每⼀一⾏行显⽰示的内容(tableView每⼀一⾏行都是UITableViewCell)
- (UITableViewCell *)tablView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- 告诉tableView每⼀组头部显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- 告诉tableView每⼀组尾部显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
tableView常见设置
-
tableView的样式
//tableView有两种样式:
tableView.style = UITableViewStyleGrouped;//分组样式
tableView.style = UITableViewStylePlain;//单组数据
-
设置tableView每一行cell的高度,默认是44
self.tableView.rowHeight = 80;
- 设置tableView每一组头部的高度
self.tableView.sectionHeaderHeight = 50;
- 设置tableView每一组尾部的高度
self.tableView.sectionFooterHeight = 50;
- 设置分割线的颜⾊,如果设置[UIColor clearColor]隐藏分割线
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];
tableViewCell常见设置
- 设置cell右边的指⽰示控件
cell.accessoryView = [[UISwitch alloc] init];
- 设置cell右边的指⽰示样式(accessoryView优先级 > accessoryType)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- 设置cell的背景view
backgroundView优先级 > backgroundColor
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor blueColor];
cell.backgroundView = bg;
- 设置cell的背景颜⾊
cell.backgroundColor = [UIColor redColor];
- 设置cell选中的背景view
UIView *selectbg = [[UIView alloc] init];
selectbg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectbg;
- 设置cell选中的样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
tableView代理方法
- 当选中某⼀行cell就会调⽤
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
- 当取消选中某⼀行cell就会调⽤
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:
(NSIndexPath *)indexPat
- 返回每一组显示的头部控件
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
(NSInteger)section
- 返回每⼀组显示的尾部控件
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:
(NSInteger)section
- 返回每⼀组头部的⾼度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:
(NSInteger)section
- 返回每⼀组尾部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
- 返回tableView每⼀行的⾼度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
性能优化
-
传统写法
//每当有一个cell进入视野范围内就会调用一次
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.定义一个重用标识
static NSString *ID = @"wine";
//2.首先去缓存池查找可循环利⽤的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//3.如里缓存池中没有,自己创建
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//4.设置数据
cell.textLabel.text = [NSString stringWithFormat:@"%ld行数据",indexPath.row];
return cell;
}
}
-
注册
NSString *ID = @"id";
- (void)viewDidLoad {
[super viewDidLoad];
// 根据ID 这个标识 注册对应的 cell类型
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
//每当有一个cell进入视野范围内就会调用一次
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.首先去缓存池查找可循环利⽤的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2.设置数据
cell.textLabel.text = [NSString stringWithFormat:@"%zd行数据",indexPath.row];
return cell;
}
索引条
//返回每一组的索引标题(数组中都是NSString对象)
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView
*)tableView