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 2018-03-19 20:26  低头捡石頭  阅读(25)  评论(0编辑  收藏  举报

导航