以下为基本展示,若有其他需求,可根据需求自行调试
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *detailTableView;
NSMutableArray *selectedArr;//控制列表是否被打开(数组中是否包含该章节,若包含,则关闭,反则展开)
NSMutableArray *_sectionArray;//章节array
NSMutableArray *cellArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
selectedArr=[[NSMutableArrayalloc]init];
_sectionArray = [[NSMutableArrayalloc]initWithObjects:@"章节1",@"章节2",@"章节3",@"章节4",@"章节5",nil];
cellArray = [[NSMutableArrayalloc]initWithObjects:@"cell1",@"cell2",@"cell3",nil];
[self creatUI];
//设置默认全关闭状态
for(int i =0; i < [_sectionArraycount]; i ++)
{
[selectedArraddObject:[NSStringstringWithFormat:@"%ld",(long)i]];
}
}
#pragma mark TableVIewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *indexStr = [NSStringstringWithFormat:@"%ld",(long)section];
//只需要给当前分组展开的section添加用户信息即可
if ([selectedArrcontainsObject:indexStr]) {
return 0;
}else
{
return [cellArraycount];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 51;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentifier =@"foundCityCellDetail";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];
cell.selectionStyle =UITableViewCellSelectionStyleNone;
}
NSString *indexStr = [NSStringstringWithFormat:@"%ld",(long)indexPath.section];
//只需要给当前分组展开的section添加用户信息即可
if (![selectedArrcontainsObject:indexStr]) {
cell.textLabel.text = [cellArrayobjectAtIndex:indexPath.row];
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_sectionArraycount];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerVIew = [[UIViewalloc]initWithFrame:CGRectMake(0,0, tableView.frame.size.width,44)];
headerVIew.backgroundColor = [UIColorwhiteColor];
UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(5,15, 15, 15)];
imageView.tag=20000+section;
//更具当前是否展开设置图片
NSString *string = [NSStringstringWithFormat:@"%ld",(long)section];
if ([selectedArrcontainsObject:string]) {
imageView.image=[UIImageimageNamed:@"grayNarrowRight"];
}else{
imageView.image=[UIImageimageNamed:@"grayNarrowDown"];
}
[headerVIew addSubview:imageView];
UILabel *titleLabel=[[UILabelalloc]initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width +10, 0, 170, headerVIew.frame.size.height)];
[headerVIew addSubview:titleLabel];
//添加一个button用来监听点击分组,实现分组的展开关闭。
UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(0,0, tableView.frame.size.width -80, 50);
btn.tag=10000+section;
[btn addTarget:selfaction:@selector(btnOpenList:)forControlEvents:UIControlEventTouchDown];
[headerVIew addSubview:btn];
UILabel *botLine = [[UILabelalloc]initWithFrame:CGRectMake(0,43.5, self.view.frame.size.width,0.5)];
botLine.backgroundColor =[UIColorgrayColor];
[headerVIew addSubview:botLine];
titleLabel.text = [_sectionArrayobjectAtIndex:section];
return headerVIew;
}
#pragma mark 章节section点击
-(void)btnOpenList:(UIButton *)sender
{
NSString *string = [NSStringstringWithFormat:@"%ld",(long)sender.tag-10000];
//数组selectedArr里面存的数据和表头想对应,方便以后做比较
if ([selectedArrcontainsObject:string])
{
[selectedArrremoveObject:string];
}
else
{
[selectedArraddObject:string];
}
[detailTableViewreloadData ];
}
#pragma mark -
#pragma mark 绘图
-(void) creatUI
{
self.view.backgroundColor = [UIColorwhiteColor];
detailTableView = [[UITableViewalloc] initWithFrame:CGRectZerostyle:UITableViewStylePlain];
detailTableView.dataSource =self;
detailTableView.delegate =self;
detailTableView.separatorStyle =UITableViewCellSeparatorStyleNone;
detailTableView.backgroundColor = [UIColorwhiteColor];
detailTableView.tableFooterView = [[UIViewalloc]init];
[self.viewaddSubview:detailTableView];
detailTableView.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end