[置顶]iOSDay29之UITableView

1. UITableView的概念

 1> 概述

  • UITableView 继承于 UIScrollView , 可以滚动
  • UITableView每一条数据对应的单元格叫做 Cell , 是 UITableViewCell 的一个对象, 继承于 UIView
  • UITableView 可以分区显示, 每个分区称为 section , 每一行称为 row, 编号都从0开始
  • 系统提供了一个专门的类来整合 sectionrow, 叫做 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协议方法的实现代码

  • UITableViewDataSource 是负责给 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 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UITableView的section中添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议中实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格中的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section中的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个section的UITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子中,我们创建了一个包含两个section的UITableView。每个section都有一个字符串数组,我们将其存储在data数组中。在numberOfSections方法中,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法中,我们返回特定section中的行数。最后,在tableView(_:cellForRowAt:)方法中,我们获取特定indexPath的字符串并将其显示在UITableViewCell中。 注意,在上述示例代码中,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件中对应的UITableViewCell的标识符也设置为“Cell”。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值