一创建tableview
playcount = 900;
tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 80,self.view.frame.size.width,self.view.frame.size.height-80) style:UITableViewStyleGrouped];
tableview.dataSource = self;
tableview.delegate = self;
//自定义高度
tableview.estimatedRowHeight = 200;
tableview.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:tableview];
二.代理方法
//组的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//每组行树
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (fold[section]==0) {
return 0;
}else{
return DataArray.count;
}
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
playcount = indexPath.section;
if (fold[indexPath.section]==0) {
return 0;
}else{
return 60;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.textLabel.text = DataArray[indexPath.row];
return cell;
}
//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//
// NSString *string = [NSString stringWithFormat:@"我时第%ld组的组尾标题",section];
// return string;
//}
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//
// NSString *string = [NSString stringWithFormat:@"我时第%ld组的组头标题",section];
// return string;
//}
//设置组头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn = [[UIButton alloc]init];
if (playcount==900) {
[btn setTitle:@"展开" forState:UIControlStateNormal];
}else{
[btn setTitle:@"收起" forState:UIControlStateNormal];
playcount = 900;
}
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
btn.tag = 500+section;
[btn addTarget:self action:@selector(cellSectionBtn:) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"hello");
return btn;
}
//组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
//组头安妮实现闭合
-(void)cellSectionBtn:(UIButton *)slend{
NSInteger i = slend.tag-500;
if (playcount==900||playcount==i) {
fold[i] = !fold[i];
//刷新某一组
NSIndexSet *indexset = [[NSIndexSet alloc]initWithIndex:i];[tableview reloadSections:indexset withRowAnimation:UITableViewRowAnimationFade];
//刷新莫一行
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:3 inSection:0];
[tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationFade];
}else{
fold[playcount]=0;
NSIndexSet *indexset1 = [[NSIndexSet alloc]initWithIndex:playcount];
[tableview reloadSections:indexset1 withRowAnimation:UITableViewRowAnimationFade];
}
}