一如既往的精简不啰嗦,直接上代码,如有问题可以给我留言.
1,创建控制器 :
ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m文件
#import "ViewController.h"
#import "AKTableViewFold.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *cellArray = @[@{@"sectionName":@"用户管理",
@"sectionArray":@[@"岗位配置",@"人员配置"]},
@{@"sectionName":@"流程管理",
@"sectionArray":@[@"流程管理"]},
@{@"sectionName":@"物资管理",
@"sectionArray":@[@"物资管理"]},
@{@"sectionName":@"公司管理",
@"sectionArray":@[@"修改公司信息",@"同级公司管理"]}];
AKTableViewFold *foldCell = [[AKTableViewFold alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
foldCell.tableArray = cellArray;
[self.view addSubview:foldCell];
}
@end
2,新建TableView类:
AKTableViewFold.h文件
#import <UIKit/UIKit.h>
@interface AKTableViewFold : UITableView<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSArray *tableArray;
@end
AKTableViewFold.m文件
#import "AKTableViewFold.h"
@interface AKCellHeaderVeiw : UITableViewHeaderFooterView
@property (nonatomic,copy) NSString *sectionTitle;
@property (nonatomic,strong) UILabel *sectionLabel;
@property (nonatomic,strong) UIButton *foldBtn;
@property (nonatomic,copy) void(^clickBtn)(UIButton *);
@end
@implementation AKCellHeaderVeiw
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self){
_sectionLabel = [[UILabel alloc]init];
_foldBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_foldBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_sectionLabel];
[self.contentView addSubview:_foldBtn];
}
return self;
}
- (void)clickButton:(UIButton *)btn{
if (_clickBtn){
self.clickBtn(btn);
}
NSLog(@"UOU");
}
- (void)layoutSubviews{
[super layoutSubviews];
_sectionLabel.frame = CGRectMake(10, 0, 100, 50);
_foldBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 60, 0, 50, 50);
}
-(void)setSectionTitle:(NSString *)sectionTitle{
_sectionTitle = sectionTitle;
_sectionLabel.text = sectionTitle;
}
@end
@interface AKTableViewFold ()
@property (nonatomic,strong) NSMutableArray *remeberArray;
@end
@implementation AKTableViewFold
- (NSArray *)remeberArray{
if (!_remeberArray){
_remeberArray = [NSMutableArray array];
for (int i = 0;i<_tableArray.count;i++){
[_remeberArray addObject:@(0)];
}
}
return _remeberArray;
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
self = [super initWithFrame:frame style:style];
if (self){
[self registerClass:[AKCellHeaderVeiw class] forHeaderFooterViewReuseIdentifier:@"HEADVIEW"];
[self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELLKUTURE"];
self.delegate = self;
self.dataSource = self;
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _tableArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([_remeberArray[section] integerValue] == 1){
return [_tableArray[section][@"sectionArray"] count];
}else{
return 0;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
AKCellHeaderVeiw *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HEADVIEW"];
headView.sectionTitle = _tableArray[section][@"sectionName"];
__weak typeof(self) weakSelf = self;
headView.clickBtn = ^(UIButton *btn){
weakSelf.remeberArray[section] = @(![weakSelf.remeberArray[section] integerValue]);
[tableView reloadData];
};
if ([weakSelf.remeberArray[section] integerValue]){
[headView.foldBtn setImage:[UIImage imageNamed:@"offline_clarity_green_up_iphone"] forState:UIControlStateNormal];
}else if (![weakSelf.remeberArray[section] integerValue]){
[headView.foldBtn setImage:[UIImage imageNamed:@"offline_clarity_green_down_iphone"] forState:UIControlStateNormal];
}
return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLKUTURE"];
cell.textLabel.text = _tableArray[indexPath.section][@"sectionArray"][indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
@end