(1).自定义页眉和页脚时,要先注册
//注册页眉
[_tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];
//注册页脚
[_tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"footer"];
(2).在UITableViewDelegate的代理方法中,分别选择-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 和-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section两个方法
//自定义页眉
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSDictionary *bigDic = bigArr[section];
UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
//商店图标
UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(SP_W(10), SP_W(10), SP_W(20), SP_W(20))];
iconImage.backgroundColor = [UIColor redColor];
[headerView addSubview:iconImage];
//商店名称
UILabel *shopLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iconImage.frame) + SP_W(5), SP_W(5), SP_W(100), SP_W(30))];
shopLabel.text = bigDic[@"tc_name"];
shopLabel.font = [UIFont systemFontOfSize:15];
[headerView addSubview:shopLabel];
return headerView;
}
//自定义页脚
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
NSDictionary *bigDic = bigArr[section];
UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"footer"];
//合计数量
UILabel *sumAmountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 8 * 4,SP_W(5), WIDTH / 8 * 3, SP_W(30))];
[footerView addSubview:sumAmountLabel];
return footerView;
}
(3).设置页眉和页脚的高度
//返回页眉的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
//返回页脚的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
NSDictionary *bigDic = bigArr[section];
return 90;
}
(4).在布局tableview的代码中也要设置页眉和页脚的高度
_tableView.sectionHeaderHight = 40;
_tableView.sectionFooterHight = 90;