UI定制 - 实现UITableViewCell的展开、闭合功能

■ 展开/闭合

1. 思路

首先使用数组 openArr 记录单元格的行号:打开单元格时(首次点击)则将其下标添加进该数组;闭合单元格时(再次点击)将其下标从数组中移除

之后根据下标是否存在去更新单元格高度,实现展开/闭合的效果

2. 具体实现

复制代码
 1 #import "ViewController.h"
 2 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
 3 @property(nonatomic,strong)UITableView *tableView;
 4 @property(nonatomic,strong)NSMutableArray *openArr; // 存放被打开的行号
 5 @property(nonatomic,strong)NSMutableArray *titArr;  // 存放标题
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     self.view.backgroundColor = [UIColor redColor];
13     
14     // UITableView
15     self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
16     self.tableView.dataSource = self;
17     self.tableView.delegate = self;
18     [self.view addSubview:self.tableView];
19     
20     // 标题
21     self.titArr = [NSMutableArray arrayWithObjects:@"申请人",@"场次时间",@"票价",@"座位",@"状态",@"备注",nil];
22 }
23 
24 // 懒加载
25 - (NSMutableArray *)openArr{
26     
27     if (_openArr == nil) {
28         _openArr = [NSMutableArray array];
29     }
30     return _openArr;
31 }
32 
33 // 多少行
34 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
35     
36     return self.titArr.count;
37 }
38 
39 // 单元格
40 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
41     
42     static NSString *cellIdentifier = @"baseCell";
43     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
44     if (!cell) {
45         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
46     }
47     // 随机色
48     cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0];
49     // 标题赋值
50     cell.textLabel.text = [self.titArr objectAtIndex:indexPath.row];
51     //
52     if ([self.openArr containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]]) {
53         cell.textLabel.text = [NSString stringWithFormat:@"第 %d 行展开",(int)indexPath.row+1];
54     }
55     return cell;
56 }
57 
58 // 高度
59 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
60     
61     // 展开高度
62     if ([self.openArr containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]]) {
63         return 100;
64     }
65     // 闭合高度
66     return 30;
67 }
68 
69 // 监听 cell 的闭合
70 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
71     
72     // 除了第 0 行,其它允许展开/闭合
73     if (indexPath.row !=0) {
74         
75         if ([self.openArr containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]]) {
76             // 之前已经点击过的,从数组中移除即可
77             [self.openArr removeObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
78         }else {
79             // 第一次点击则添加进数组
80             [self.openArr addObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
81         }
82         // 刷新指定 cell
83         [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
84     }
85     NSLog(@"本次点击的行号:%d,%@",(int)indexPath.row,self.openArr);
86 }
87 
88 @end
复制代码

运行效果:展开 第2行 和 第5行    |    关闭 第5行

       |     

 

posted on   低头捡石頭  阅读(34)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示