一、设置表头
1. 设置表头高度
//设置表头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0f;
}
2. 添加标头中的内容
//添加标头中的内容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *headerSectionID = @"headerSectionID";
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerSectionID];
UILabel *label;
if (headerView == nil) {
headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerSectionID];
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
label.font = FONT(13);
[headerView addSubview:label];
}
if (section == 0) {
label.text = @"我的群组";
}else {
label.text = @"我的好友";
}
return headerView;
}
这里用到了表头复用的方法。
要先设置表头高度,才会调用设置表头内容的方法。
二、表脚
1. 设置表脚高度
//设置表尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 20.0f;
}
2. 添加表脚内容
//添加标脚中的内容
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
static NSString *footSectionID = @"footSectionID";
UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:footSectionID];
UILabel *label;
if (footerView == nil) {
footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:footSectionID];
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
label.font = FONT(13);
[footerView addSubview:label];
}
if (section == 0) {
label.text = @"我的群组";
}else {
label.text = @"我的好友";
}
return footerView;
}
这里用到了表脚复用的方法。
要先设置表脚高度,才会调用设置表脚内容的方法。