@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
/** UITableView*/
@property(nonatomic,weak) UITableView * tableView;
/** 分组头数据*/
@property(nonatomic,strong) NSMutableArray * sectionArray;
/** 数据数组*/
@property(nonatomic,strong) NSArray * dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载数据
[self _loadData];
UITableView * table=[[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStyleGrouped];
table.delegate=self;
table.dataSource=self;
self.tableView=table;
table.sectionFooterHeight=0;
// table.sectionHeaderHeight = 30;
[self.view addSubview:table];
}
//加载数据
-(void)_loadData{
NSArray * array=@[@"分组1",@"分组2",@"分组3",@"分组4",@"分组5"];
self.sectionArray=[NSMutableArray arrayWithArray:array];
NSString * path=[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
self.dataArray=[NSArray arrayWithContentsOfFile:path];
}
#pragma mark - UITableViewDatasource
//返回行数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary * temDic=self.dataArray[section];
NSArray * temArray=temDic[@"friends"];
return temArray.count;
}
//返回cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identy=@"table";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
}
NSDictionary * temDic=self.dataArray[indexPath.section];
NSArray * temArray=temDic[@"friends"];
cell.textLabel.text=temArray[indexPath.row];
return cell;
}
//返回分组数量
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return self.dataArray.count;
}
#pragma mark - UITableViewDelegate
- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSDictionary * temDic=self.dataArray[section];
return temDic[@"group"];
}
//设置尾部的标题,即使footer高度设置为0,也会显示
//- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//return @"hello world";
//}
#pragma mark * 自定义头部 自定义的优先级高,即使footer高度设置为0,也会显示(代理优先)
//- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//
// UIView * view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
// view.backgroundColor=[UIColor redColor];
//
// return view;
//}
#pragma mark * 自定义尾部
//- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//
// UIView * view=[[UIView alloc] init];
// view.backgroundColor=[UIColor blueColor];
// return view;
//}
#pragma mark * 设置头部的高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
#pragma mark * 设置尾部的高度
//- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
// return 30;
//}
//设置右侧的索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray * array=[NSMutableArray array];
for (NSDictionary *dic in self.dataArray) {
[array addObject:dic[@"group"]];
}
return array;
}