本文实例为大家分享了ios实现折叠单元格的具体代码,供大家参考,具体内容如下
思路
点击按钮或cell单元格来进行展开收缩, 同时使用一个bool值记录单元格展开收缩状态。根据bool值对tableview的高度和button的image进行实时变更。
注意点:
在执行- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath( 点击当前单元格)方法时,收缩单元格,显示当前点击的单元格的内容。这一步骤的实现是对存储单元格内容的可变数组进行更改。
代码
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//viewcontroller.h 中
#import <uikit/uikit.h>
@interface viewcontroller : uiviewcontroller
@property uitableview *tableview;
@property uibutton *button;
@property nsmutablearray *imageviewarr;
@property nsmutablearray *labelarr;
@property bool select; //记录单元格展开收缩状态
@end
|
?
|
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
//viewcontroller.m 中
#import "viewcontroller.h"
#import "viewtableviewcell.h"
#import "masonry.h"
@interface viewcontroller () <uitableviewdelegate, uitableviewdatasource>
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
self.view.backgroundcolor = [uicolor colorwithwhite:0.92 alpha:1];
_imageviewarr = [[nsmutablearray alloc] initwithobjects:@"1", @"2", @"3", @"4", @"5", nil];
_labelarr = [[nsmutablearray alloc] initwithobjects:@"发起群聊", @"添加朋友", @"扫一扫", @"收付款", @"帮助与反馈", nil];
_tableview = [[uitableview alloc] init];
[self.view addsubview:_tableview];
_tableview.frame = cgrectmake(100, 100, 130, 35);
//以下使用masonry对tableview进行约束, 约束不是很规范 可忽略
// [_tableview mas_makeconstraints:^(masconstraintmaker *make) {
// make.height.mas_offset(self.view.frame.size.height * 0.0485);
// make.width.mas_offset(self.view.frame.size.width * 0.335);
// make.left.equalto(self.view.mas_left).offset(self.view.frame.size.width * 0.6);
// make.top.equalto(self.view.mas_top).offset(self.view.frame.size.height * 0.046);
//
// }];
_tableview.delegate = self;
_tableview.datasource = self;
[_tableview registerclass:[viewtableviewcell class] forcellreuseidentifier:@"cell"];
_button = [uibutton buttonwithtype:uibuttontypecustom];
[self.view addsubview:_button];
[_button mas_makeconstraints:^(masconstraintmaker *make) {
make.left.equalto(_tableview.mas_right).offset(-28);
make.top.equalto(_tableview.mas_top).offset(4);
make.height.mas_offset(self.view.frame.size.height * 0.0495 * 0.68);
make.width.mas_offset(self.view.frame.size.width * 0.335 * 0.22);
}];
[_button setimage:[uiimage imagenamed:@"shou"] forstate:uicontrolstatenormal];
[_button addtarget:self action:@selector(press) forcontrolevents:uicontroleventtouchupinside];
//默认单元格为收缩 select为0
_select = 0;
}
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {
return 1;
}
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
//根据select的值来判断收缩展开状态,返回相应的行数
if(_select == 0) {
return 1;
} else {
return 5;
}
}
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath {
return 40;
}
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
viewtableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath];
cell.iimageview.image = [uiimage imagenamed:_imageviewarr[indexpath.row]];
cell.label.text = [nsstring stringwithstring:_labelarr[indexpath.row]];
return cell;
}
//点击当前单元格
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {
//记录当前单元格的imageview 和 label的内容
nsstring *imageviewstr = [nsstring stringwithstring:_imageviewarr[indexpath.row]];
nsstring *labelstr = [nsstring stringwithstring:_labelarr[indexpath.row]];
//将当前单元格的内容插入可变数组,作为第一个元素
[_imageviewarr insertobject:imageviewstr atindex:0];
[_labelarr insertobject:labelstr atindex:0];
//同时删除可变数组中当前单元格的原本所在位置
[_imageviewarr removeobjectatindex:indexpath.row + 1];
[_labelarr removeobjectatindex:indexpath.row + 1];
//更新tableview
[_tableview reloaddata];
//调用press方法, 变更tableview的高度 和 button的image
[self press];
}
- (void)press {
//通过判断select的值, 判断单元格的展开与收缩,更改tableview的高度 和 button的image
if (_select == 0) {
_select = 1;
_tableview.frame = cgrectmake(100, 100, 130, 200);
//以下使用masonry对tableview进行更新约束 (以下代码为更新tableview的高度)
// [_tableview mas_updateconstraints:^(masconstraintmaker *make) {
// make.height.mas_offset(200);
// }];
[_button setimage:[uiimage imagenamed:@"kai"] forstate:uicontrolstatenormal];
} else {
_select = 0;
_tableview.frame = cgrectmake(100, 100, 130, 35);
// [_tableview mas_updateconstraints:^(masconstraintmaker *make) {
// make.height.mas_offset(self.view.frame.size.height * 0.0485);
// }];
[_button setimage:[uiimage imagenamed:@"shou"] forstate:uicontrolstatenormal];
}
[_tableview reloaddata];
}
@end
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
// viewtableviewcell.h 中
#import <uikit/uikit.h>
ns_assume_nonnull_begin
@interface viewtableviewcell : uitableviewcell
@property uiimageview *iimageview;
@property uilabel *label;
@end
|
?
|
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
|
//viewtableviewcell.m中
#import "viewtableviewcell.h"
@implementation viewtableviewcell
- (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier {
self = [super initwithstyle:style reuseidentifier:reuseidentifier];
_iimageview = [[uiimageview alloc] init];
[self.contentview addsubview:_iimageview];
_label = [[uilabel alloc] init];
[self.contentview addsubview:_label];
return self;
}
- (void)layoutsubviews {
[super layoutsubviews];
_iimageview.frame = cgrectmake(5, 5, 25, 25);
_label.frame = cgrectmake(37, 5, 80, 25);
_label.font = [uifont systemfontofsize:15];
}
@end
|
效果图如下
初始状态
点击cell或点击按钮,显示如下:
点击任意cell, 例如点击扫一扫,单元格收回,如图
再次展开单元格, cell的内容如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
Linux MyEclipse启动Tomcat太慢内存和cpu被大量占用
2025-05-27 53 -
2025-05-29 32
-
2025-05-27 28
-
2025-06-04 28
-
2025-05-25 49
热门评论





