<UITableViewDelegate,UITableViewDatasource>
- (void)_creatTableView{
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 100) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor yellowColor];
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentify];
[self.view addSubview:_tableView];
}
#pragma mark UITableViewDelegate
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 6;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headView = [[UIView alloc]init];
headView.backgroundColor = [UIColor whiteColor];
UILabel *headTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 20, 20)];
headTitle.text = [NSString stringWithFormat:@"%ld",section];
[headView addSubview:headTitle];
//下拉按钮的设置
UIButton* pullDownbtn =[UIButton buttonWithType:UIButtonTypeCustom];
pullDownbtn.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 20, 20);
//[pullDownbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
pullDownbtn.tag = section;
[pullDownbtn addTarget:self action:@selector(pullDownAction:) forControlEvents:UIControlEventTouchUpInside];
[headView addSubview:pullDownbtn];
//下拉按钮图标
UIImageView *pullDownbtnView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, pullDownbtn.size.width, pullDownbtn.size.height)];
// pullDownbtnView.image = [UIImage imageNamed:@"CHANGJIN_icon_all@2x.png"];
pullDownbtnView.backgroundColor = [UIColor redColor];
[pullDownbtn addSubview:pullDownbtnView];
return headView;
}
#pragma mark 组头视图上的按钮点击事件
- (void)pullDownAction:(UIButton *)sender{
NSInteger section = sender.tag;
//改变数组中元素的状态
flag[section] = !flag[section];
NSIndexSet *set = [NSIndexSet indexSetWithIndex:section];
[_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
[_tableView reloadData];
}
#pragma mark 设置不同的组有不同的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// BOOL f = flag[section]; //NO :展开
// if(!f){
// return 0; //场景列表不展开
// }else{
// for (NSInteger i = 0; i < [titleArray count]; i++) {
// if (section == i) {
// return [[dataArray objectAtIndex:section]count];
// }
// }
// return [[dataArray objectAtIndex:section]count];
// }
BOOL f = flag[section]; //NO :展开
if(!f){
return 0; //场景列表不展开
}else{
return 3;
}
}
#pragma mark 组头视图 高度为60
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify forIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
return cell;
}