前言
- 在暑期学习完成3Gshare的时候有一个界面需要使用折叠cell,在这里把那部分的折叠cell详细解释一下
3Gshare的引入
- 如图所示,点开展示折叠cell的内容,收起展示点击的选项即可
思路
- 上面展示的是把展开button和收起button做成cell放上去,这里做成imageButton。
- 设置一个存放内容的数组
arrayCell
,设置一个tableView
,一个按钮ButtonChange
,一个当前CellcellCure
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *ButtonChange;
@property (nonatomic, strong) NSMutableArray *arrayCell;
@property (nonatomic, strong) UITableViewCell *cellCure;
- 点击ButtonChange
-
- 改变tableView的宽度,并且改变图标(记得更新数据)
- (void)pressButton :(UIButton*) button{
if (_ButtonChange.tag == 1) {
_tableView.frame = CGRectMake(230, 180, 120, 120);
[_ButtonChange setImage:[UIImage imageNamed:@"buttonno.png"] forState:UIControlStateNormal];
_ButtonChange.tag = 2;
[_tableView reloadData];
} else {
_tableView.frame = CGRectMake(230, 180, 120, 35);
[_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
_ButtonChange.tag = 1;
[_tableView reloadData];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_cellCure = [_tableView cellForRowAtIndexPath:indexPath];
NSString* string = [NSString stringWithString:_cellCure.textLabel.text];
for (int i = 0; i < _arrayCell.count; i ++) {
if ([string isEqualToString:_arrayCell[i]]) {
_arrayCell[i] = _arrayCell[0];
break;
}
}
_arrayCell[0] = string;
_tableView.frame = CGRectMake(230, 180, 120, 25);
[_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
_ButtonChange.tag = 1;
[_tableView reloadData];
}
- 注意对于上述代码的
for (int i = 0; i < _arrayCell.count; i ++) { if ([string isEqualToString:_arrayCell[i]]) { _arrayCell[i] = _arrayCell[0]; break; } }
部分
-
- 是为了保持数组里面的元素不变,达成一泓交换,相当于把当前cell的内容赋值给选择的cell,然后选择的cell就会跑到第一个cell的位置,之后折叠即可显示选择的cell
完整代码
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *ButtonChange;
@property (nonatomic, strong) NSMutableArray *arrayCell;
@property (nonatomic, strong) UITableViewCell *cellCure;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(230, 180, 120, 25) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
[self.view addSubview:_tableView];
_ButtonChange = [[UIButton alloc] init];
_ButtonChange.frame = CGRectMake(200, 180, 30, 30);
[_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
[_ButtonChange addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
_ButtonChange.tag = 1;
[self.view addSubview:_ButtonChange];
_arrayCell = [NSMutableArray arrayWithObjects:@"iOS", @"macOS", @"3G", @"Cell", nil];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
cell.textLabel.text = _arrayCell[indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (_ButtonChange.tag == 1) {
return 1;
} else {
return 4;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 30;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_cellCure = [_tableView cellForRowAtIndexPath:indexPath];
NSString* string = [NSString stringWithString:_cellCure.textLabel.text];
for (int i = 0; i < _arrayCell.count; i ++) {
if ([string isEqualToString:_arrayCell[i]]) {
_arrayCell[i] = _arrayCell[0];
break;
}
}
_arrayCell[0] = string;
_tableView.frame = CGRectMake(230, 180, 120, 25);
[_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
_ButtonChange.tag = 1;
[_tableView reloadData];
}
- (void)pressButton :(UIButton*) button{
if (_ButtonChange.tag == 1) {
_tableView.frame = CGRectMake(230, 180, 120, 120);
[_ButtonChange setImage:[UIImage imageNamed:@"buttonno.png"] forState:UIControlStateNormal];
_ButtonChange.tag = 2;
[_tableView reloadData];
} else {
_tableView.frame = CGRectMake(230, 180, 120, 35);
[_ButtonChange setImage:[UIImage imageNamed:@"buttonyes.png"] forState:UIControlStateNormal];
_ButtonChange.tag = 1;
[_tableView reloadData];
}
}
@end