自定义表头部分

Eug*_*kov 4 uitableview ios

我有一个团体风格表的应用程序.

我试图自定义表头部分:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake (0,0,200,30)];
    if (section != 0)userName.text = @"lalala";
    [userName setFont:[UIFont fontWithName:@"HelveticaNeue" size:20]];
    [headerView addSubview:userName];
    [headerView setBackgroundColor:[UIColor clearColor]];
    [userName setBackgroundColor:[UIColor clearColor]];
    return headerView;
}
Run Code Online (Sandbox Code Playgroud)

但我的标题关闭了细胞:

在此输入图像描述

为什么?

Ian*_*n L 16

您还需要实现tableView:heightForHeaderInSection:为节标题设置正确的高度.请参阅Apple文档


Cth*_*utu 5

发生这种情况是因为标头的高度设置为0,直到您覆盖另一个委托:

- tableView:heightForHeaderInSection: