#对屏幕的宽和高取别名
#define screenWidth [UIScreen mainScreen].bounds.size.width

#define screenHeight [UIScreen mainScreen].bounds.size.height



#签数据源协议代理

@interface ViewController()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSArray *dataArray;

@end





#pragma mark - 加载数据

- (void)_loadData {
    //拼接路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"font.plist" ofType:nil];
    _dataArray = [NSArray arrayWithContentsOfFile:path];
}

#pragma mark - 加载子视图
- (void)_loadTableView {
   
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
   
    tableView.dataSource = self;
    tableView.delegate = self;
   
//    self.tableView = tableView;
    [self.view addSubview:tableView];
   
//注册单元格
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
   
//注册 每组头视图的类型
    [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"hfID"];
   
}

#pragma mark - datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
    return _dataArray.count;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //根据哪一组来返回具体的行数
    //获取到是第几组
    NSArray *fontArray = _dataArray[section];
    //返回行数
    return fontArray.count;
}


//返回每行单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    //写这个方法 前面一定要先注册了单元格类型
    //因为这个方法 它内部 是先去闲置池中找对应的Identifier的单元格 如果说没有找到 这个可复用的单元格 那么它内部 会根据 你在前面注册时 class 的类型 Identifier 来创建单元格  。直到它可以从闲置池中取到 可复用的单元格。
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  只是去找 它不会帮你创建
   
   
    NSArray *fontArray = _dataArray[indexPath.section];
   
    cell.textLabel.text = fontArray[indexPath.row];
   
//演示添加到cell上的控件 如果写在这里 会不断地添加
//  UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 100, 10, 80, 30)];
//    [cell.contentView addSubview:sw];
   
   
   
    return cell;
   
}

#pragma mark - delegate
//设置每组头视图的标题  和下面的设置头视图的方法 两个只会有一个起作用
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    return @"头部";
//}

//调用这个方法 返回一个视图 那么这个视图就会被添加到对应组的头部 作为头视图显示
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   
    //没有复用的版本0
    /*
    static int count = 0;
   
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 100)];
    label.text = [NSString stringWithFormat:@"%ld组的头视图",section];
    label.backgroundColor = [UIColor yellowColor];
   
    count ++;
   
    NSLog(@"count : %d",count);
   
    return label;
     */
   
    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"hfID"];
    //前面如果没有注册
    /*
    if (view == nil) {
        view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"hfID"];
        //... 当复用的头视图 足够了 Label就不会被创建了  因为view!=nil 所以这里面的代码不会走了
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 100)];
        label.backgroundColor = [UIColor yellowColor];
       
        [view.contentView addSubview:label];
    }
     */
   
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 100)];
    label.text = [NSString stringWithFormat:@"%ld组的头视图",section];
    label.backgroundColor = [UIColor yellowColor];
   
    [view.contentView addSubview:label];
   
   
    if (view.subviews.count > 2) {
        [label removeFromSuperview];
    }
   
    NSLog(@"%ld",view.subviews.count);
   
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   
    if (section == 2) {
        return 300;
    }
   
    return 100;
}