2009-06-03 187 views
5

对于iPhone,我有一个UITableView被分组,有一个部分,并在其中设置了一个节头,它是来自nib的UILabel对象。显示表格视图时,标题显示为纯黑色的条纹 - 没有文字。UITableView节头是全黑

在heightForHeaderInSection我已经设置高度为UILabel对象的frame.size.height。当我改变IB的高度时,黑色条纹的高度会发生变化。所以我知道.m文件锁定在右侧的UILabel对象上。

在调试器中,在viewForHeaderInSection中,似乎UILabel对象的宽度为零,高度为1079574528,文本为空。

有关我在做什么错的任何想法?

回答

0

你能后的代码为您heightForHeaderInSection和你viewForHeaderInSection的功能呢?你所做的背后的理论听起来是正确的,但没有看到代码,这是几乎不可能找出问题...

这听起来像你在IB的视图上放置一个标签,并试图用它作为你的标题视图 - 这不是正确的做事方式。如果你不使用viewForHeaderInSection,然后给一个尝试..这样的:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UILabel *lbl; 
    lbl.text = @"Header for The Only Section"; 
    //define other properties for the label - font, shadow, highlight, etc... 

    return lbl; 
} 
+2

这不起作用,因为您还没有初始化标签。你需要`[[UILabel alloc] initWithFrame:someFrame]`。然后,你可以设置它的`text`属性。 – 2009-12-13 19:58:22

25

不知道你做错了什么,但这里是一些示例代码,可以帮助(从post上我的博客):

#define SectionHeaderHeight 40 


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if ([self tableView:tableView titleForHeaderInSection:section] != nil) { 
     return SectionHeaderHeight; 
    } 
    else { 
     // If no section header title, no section header needed 
     return 0; 
    } 
} 


- (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] autorelease]; 
    label.frame = CGRectMake(20, 6, 300, 30); 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green 
           saturation:1.0 
           brightness:0.60 
             alpha:1.0]; 
    label.shadowColor = [UIColor whiteColor]; 
    label.shadowOffset = CGSizeMake(0.0, 1.0); 
    label.font = [UIFont boldSystemFontOfSize:16]; 
    label.text = sectionTitle; 

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

    return view; 
} 
+0

我有黑色的“酒吧”问题,但只有在切换到横向模式时,并且在返回肖像时不能解决问题。只有这样,我发现到目前为止“修复”它是通过在tableview上调用reloadData,但这是一个非常繁重的方法来调用修复这个小小的东西... – kdbdallas 2009-11-23 09:11:35

+1

单词无法表达您的代码示例为我节省了多少时间: ) **谢谢!!!! – 2011-09-11 17:49:21

1

我有同样的问题,并没有完全明白为什么黑条..

,但不是提供在委托方法页眉和页脚的意见, 如果我设置VAL它的一切都好!tableView.tableHeaderViewtableView.tableFooterView,它的一切都好!

0

3.1.3不喜欢[UIColor clearColor];尝试使用与您的桌面视图相同的背景颜色

0

我在加载数据源后刷新时观察到相同的行为。 我注意到这是由于我刷新桌子视图的方式。

//[self loadView]; this caused the section header to go black. 
[self.tableView reloadData]; // this works!