2012-04-28 72 views
5

我试图创建一个自定义的ios表标题时有点卡住了。我的标题是30像素高,我使用这个代码来创建它们:自定义标题与组中的单元格重叠

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    if (sectionTitle == nil) { 
     return nil; 
    } 

    // Create label with section title 
    UILabel *label = [[UILabel alloc] init] ; 
    label.frame = CGRectMake(0, 0, 140, 30); 
    label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"header_gradient.png"]]; 
    label.textColor = [UIColor whiteColor]; 
    label.shadowColor = [UIColor whiteColor]; 
    label.shadowOffset = CGSizeMake(0.0, 1.0); 
    label.font = [UIFont boldSystemFontOfSize:12]; 
    label.text = sectionTitle; 

    // Create header view and add label as a subview 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 30)]; 
    [view addSubview:label]; 

    return view; 
} 

- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section { 
    return 30; 
} 

它几乎工作,但我的头似乎与它们下面的细胞/头到colide:

custom headers

灿有人指出我在正确的方向清理标题在这里?

在此先感谢

回答

14

你缺少一个[空格]:

您有:

- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section { 

它应该是:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
+0

它实际上应该是' - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section' – omz 2012-04-28 05:35:36

+0

正确。正确的声明如下(从文档): - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)部分 - 如果您密切关注,tableView和heightForHeaderInSection之间缺少[空格]。完全错过了第一次,因此我认为骆驼案件是关闭的。我已经更新了答案。谢谢你的收获! – 2012-04-28 05:50:38

+0

感谢您的帮助......我疯了,试图找出为什么这是行不通的! – akhalsa 2012-04-28 19:43:15