1. UITableView的概念
2> UITableView代理的实现代码
① 签订 UITableView 协议
② 设置当前的 ViewController 为 UITableView 代理
3> UITableView 重用 cell 的两种方法
1> 概述
- UITableView 继承于 UIScrollView , 可以滚动
- UITableView 的每一条数据对应的单元格叫做 Cell , 是 UITableViewCell 的一个对象, 继承于 UIView
- UITableView 可以分区显示, 每个分区称为 section , 每一行称为 row, 编号都从0开始
- 系统提供了一个专门的类来整合 section 和 row, 叫做 NSIndexPath
2> 图解
2. UITableView的基本使用
1> UITableView 的创建代码
1 // 创建对象 2 // 使用初始化方法,并设置样式 3 // 系统为我们提供了两种样式 (UITableViewStyleGrouped 分组样式 ,UITableViewStylePlain 普通样式) 4 self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStyleGrouped]; 5 6 // 添加到父视图 7 [self addSubview:self.tableView];
2> UITableView 显示的相关属性
1 // 设置属性 2 3 // 分隔线的颜色 4 self.tableView.separatorColor = [UIColor blackColor]; 5 6 // 分隔线的样式 7 self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 8 9 // 设置行高 10 self.tableView.rowHeight = 120; 11 12 // 添加置顶视图 13 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)]; 14 15 headerView.backgroundColor = [UIColor cyanColor]; 16 17 self.tableView.tableHeaderView = headerView;
3. UITableView显示数据
1> UITableView中两个重要的属性
2> UITableView代理的实现代码
① 签订 UITableView 协议
@interface ViewController () <UITableViewDataSource, UITableViewDelegate> @end
② 设置当前的 ViewController 为 UITableView 代理
1 // 设置dataSource代理 2 tableView.delegate = self; 3 4 // 设置delegate代理 5 tableView.dataSource = self;
③ UITableViewDataSource协议方法的实现代码
- UITableView 的 DataSource 是负责给 UITableView 对象提供数据的代理, 遵循 UITableViewDataSource 协议
- UITableViewDataSource 协议中有两个必须实现的协议方法
1 // tableView每个分区要显示的行数 2 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 3 return 100; } 4 // tableView每次要显示一个cell都会调用这个方法获取 5 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 6 UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"] autorelease]; 7 cell.textLabel.text = @"标题"; 8 return cell; 9 }
- UITableViewDataSource 协议中其他的协议方法
1 // 设置分区个数 2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 3 return 2; 4 } 5 6 // 设置头标题 7 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 8 9 return @"头标题"; 10 } 11 12 // 设置未尾标题 13 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { 14 return @"尾标题"; 15 } 16 17 // 设置分区索引 18 - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { 19 20 return @[@"A", @"B", @"C", @"D"]; 21 }
④ UITableViewDelegate 协议中的协议方法
1 // 设置行高 2 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 3 return 120; 4 } 5 6 // 设置分区头高度 7 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 8 if (section == 0) { 9 return 100; 10 } 11 return 40; 12 } 13 14 // 设置分区尾高度 15 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { 16 17 return 40; 18 } 19 20 // 自定义Section的header视图 21 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 22 if (section == 0) { 23 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)]; 24 label.backgroundColor = [UIColor orangeColor]; 25 label.text = @"第一个section的header视图"; 26 27 return label; 28 } 29 return nil; 30 } 31 // 点击cell 32 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 33 // 取消选中效果 34 [tableView deselectRowAtIndexPath:indexPath animated:YES]; 35 36 if (indexPath.section == 0 && indexPath.row == 0) { 37 // 跳转第二页 38 SecondViewController *secondVC = [[SecondViewController alloc] init]; 39 [self.navigationController pushViewController:secondVC animated:YES]; 40 41 } 42 }
3> UITableViewCell
UITableView 的一个单元格 UITableViewCell 类的对象, 继承于 UIView
① UITableViewCell 默认提供了3个视图属性:
1 cell.textLabel.text = @"朋友圈"; 2 3 // 详细的描述Label 4 cell.detailTextLabel.text = @"分享"; 5 6 cell.imageView.image = [UIImage imageNamed:@"Action_Moments"];
② 设置后面的小视图
1 // 设置后面的小视图 - 系统的样式 (accessory 配件) 2 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 3 4 // 设置后面的小视图 - 自定义样式 5 UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; 6 accessoryView.image = [UIImage imageNamed:@"Expression_3"]; 7 8 cell.accessoryView = accessoryView;
4. UITableViewCell的重用机制
1> 概述
UITableView 有一个重用池机制管理 cell , 目的是使用尽可能少的 cell 显示数据
2> UITableView重用cell的流程
- 当一个 cell 被滑出屏幕, 这个 cell 会被系统放到相应的重用池中
- 当 tableView 需要显示一个 cell , 会先去重用池中尝试获取一个 cell
- 如果重用池没有 cell , 就会创建一个 cell
- 取得 cell 之后会重新赋值进行使用
3> UITableView 重用 cell 的两种方法
① 普通方法
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 2 3 static NSString *identifier = @"cell"; 4 // 1.优先从重用队列中查找标识符为cell的UITableViewCell对象 5 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 6 7 if (cell == nil) { 8 // 如果cell为空,说明重用队列中没有标识符为cell的对象,那么创建标识符为cell的对象 9 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; 10 } 11 12 // 设置cell显示的数据 13 ... 14 15 // 返回cell对象 16 return cell; 17 }
② 使用注册实现
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 // 第一步:注册cell 5 [_rootView.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; 6 7 } 8 // 返回cell对象 9 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 10 { 11 // 第二步:根据重用标识符查找cell(如果找到就返回,没有找到就会自动创建一个并返回) 12 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 13 14 // 设置数据 15 cell.textLabel.text = _dataArray[indexPath.row]; 16 17 return cell; 18 }