tableView的折叠单元格
前言
在前一阵子写share的时候遇见了tableView的折叠单元格的使用,然后我们在平时的app使用时应该也见过就是有一个按钮点击以后会出现拓展出的内容,这就需要使用tableView的折叠单元格。
用法
首先我们先创建一个YepTableViewCell继承于TableViewCell
,我们在YepTableViewCell.m文件中
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self.label = [[UILabel alloc] init];
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
_label.frame = CGRectMake(0, 0, 300, 10);
}
接着我们需要将创建好的cell应用于tableView中
首先在YepViewController.h文件中,声明所有需要用到的数据,比如可变数组,tableView,button等等,同时还需要声明一个控制tableView收缩展开的BOOL值。
#import <UIKit/UIKit.h>
#import "YepTableViewCell.h"
NS_ASSUME_NONNULL_BEGIN
@interface YepViewController : UIViewController<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic , strong) UITableView *tableView;
@property(nonatomic , strong) UIButton *button;
@property NSMutableArray *strArr;
@property NSMutableArray *str;
@property YepTableViewCell *yepTableViewCell;
@property BOOL openSelect;
@end
NS_ASSUME_NONNULL_END
然后在YepViewController.m文件中,需要做的事情是创建好可变数组,然后按钮点击事件等等
#import "YepViewController.h"
@interface YepViewController ()
@end
@implementation YepViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_openSelect = NO;
_strArr = [[NSMutableArray alloc] init];
[_strArr addObject:@"马思唯"];
[_strArr addObject:@"法老"];
[_strArr addObject:@"姜云升"];
[_strArr addObject:@"刘聪"];
[_strArr addObject:@"STA"];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(200, 100, 100, 25) style:UITableViewStylePlain];
[self.view addSubview:_tableView];
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.frame = CGRectMake(300, 100, 25, 25);
[_button setImage:[UIImage imageNamed:@"arrow-left.png"] forState:UIControlStateNormal];
[_button addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:_button];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[YepTableViewCell class] forCellReuseIdentifier:@"111"];
}
- (void)pressBtn:(UIButton *)btn {
if (_openSelect == NO) {
self.tableView.frame = CGRectMake(200, 100, 100, 100);
[btn setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
_openSelect = YES;
} else {
self.tableView.frame = CGRectMake(200, 100, 100, 25);
[btn setImage:[UIImage imageNamed:@"arrow_left.png"] forState:UIControlStateNormal];
_openSelect = NO;
}
[_tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(_openSelect == NO){
return 1;
} else {
return 5;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 25;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
_yepTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"111"];
if (!_yepTableViewCell) {
_yepTableViewCell = [[YepTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"111"];
_yepTableViewCell.selectionStyle = UITableViewCellSelectionStyleGray;
}
if (_openSelect == YES) {
_yepTableViewCell.textLabel.text = _strArr[indexPath.row];
_yepTableViewCell.textLabel.font = [UIFont systemFontOfSize:13];
} else {
_yepTableViewCell.textLabel.text = _strArr[indexPath.row];
_yepTableViewCell.textLabel.font = [UIFont systemFontOfSize:13];
}
return _yepTableViewCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_str = [NSMutableString stringWithString:_strArr[indexPath.row]];
//insertObject:atIndex:函数自动后移其他元素
[_strArr insertObject:_str atIndex:0];
//因为将原本位置的元素移动到了第一个所以原本的地方就多了一个元素,我们把原本位置的地方后移一个,从而达到删除
[_strArr removeObjectAtIndex:indexPath.row + 1];
//收缩tableView
_openSelect = NO;
//改大小
_tableView.frame = CGRectMake(200, 100, 100, 25);
[_button setImage:[UIImage imageNamed:@"arrow-left.png"] forState:UIControlStateNormal];
[_tableView reloadData];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end