2012-03-16 88 views
4

我收到了一个组风格表的应用程序。自定义表头部分

我尝试定制表头部分:

- (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; 
} 

但我的头靠近细胞:

enter image description here

为什么?

+2

仅供参考,您可以用故事板拖出一个UIView并将其放置为表格标题。然后将其分配给IBOutlet。 – Gobot 2012-03-16 14:11:57

回答

16

您还需要实施tableView:heightForHeaderInSection:以设置节标题的正确高度。见the Apple docs

+0

你是第一次:) – 2012-03-16 14:07:23

5

,因为你的头的高度,直到你覆盖其他代表被设置为0这是发生:

- 的tableView:heightForHeaderInSection: