iOS实现折叠单元格

2025-05-29 0 35

本文实例为大家分享了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

效果图如下

初始状态

iOS实现折叠单元格

点击cell或点击按钮,显示如下:

iOS实现折叠单元格

点击任意cell, 例如点击扫一扫,单元格收回,如图

iOS实现折叠单元格

再次展开单元格, cell的内容如下:

iOS实现折叠单元格

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS实现折叠单元格 https://www.kuaiidc.com/88969.html

相关文章

发表评论
暂无评论