#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
@interface CellTreesController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *MyTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;//存放标题
@property(nonatomic,strong)NSMutableDictionary *boolDcitionary;//存放对应分区section是否展开
@end
@implementation CellTreesController
- (void)viewDidLoad {
[super viewDidLoad];
_MyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
_MyTableView.dataSource = self;
_MyTableView.delegate = self;
_MyTableView.tableFooterView = [UIView new];
[self.view addSubview:_MyTableView];
//组头数据
for (int i = 0; i < 20; i ++) {
NSString *titleString = [NSString stringWithFormat:@"第%d组",i + 1];
[self.dataArray addObject:titleString];
}
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - dalegate dataSource
//区头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 44;
}
//区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 7, 150, 30)];
lable.text = self.dataArray[section];
lable.textColor = [UIColor orangeColor];
headerView.tag = 1000 + section;
//添加手势->控制cell的展开
[headerView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickView:)]];
[headerView addSubview:lable];
return headerView;
}
//section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 20;
}
//row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *clickTag = [NSString stringWithFormat:@"%ld",section + 1000];
if ([self.boolDcitionary[clickTag] integerValue] == 1) {
return 6;
}else{
return 0;
}
}
//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"myTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row + 1];
return cell;
}
#pragma mark - tap手势点击事件
- (void)clickView:(UITapGestureRecognizer *)action{
NSString *clickTag = [NSString stringWithFormat:@"%ld",action.view.tag];
//第二个cell展开时仍然保持前一个cell的展开状态
//状态为0 代表关闭
if ([self.boolDcitionary[clickTag] integerValue] == 0) {
[self.boolDciti