【问题标题】:UITableView section header is all blackUITableView 部分标题全黑
【发布时间】:2009-06-03 02:41:33
【问题描述】:

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

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

在调试器中,viewForHeaderInSection中,好像UILabel对象的宽度为零,高度为1079574528,文字为null。

对我做错了什么有什么想法吗?

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    不确定您做错了什么,但这里有一些示例代码可能会有所帮助(来自我博客上的 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;
    }
    

    【讨论】:

    • 我遇到了黑色“条”问题,但仅在切换到横向模式时才出现,并且在返回纵向时无法修复。到目前为止,我发现“修复”它的唯一方法是在 tableview 上调用 reloadData 但这是一个非常繁重的方法来调用来修复这个小东西......
    • 文字无法表达您的代码示例为我节省了多少时间 :) **谢谢!!!!
    【解决方案2】:

    我也遇到了同样的问题,但还没有完全弄清楚为什么会出现黑条..

    但是,不是在委托方法中提供页眉和页脚视图, 如果我为tableView.tableHeaderViewtableView.tableFooterView 设置值,一切都很好!

    【讨论】:

    • 为我工作。我的标题是透明的,位于行的顶部。
    【解决方案3】:

    您能否发布您的 heightForHeaderInSectionviewForHeaderInSection 函数的代码?你所做的事情背后的理论听起来是正确的,但是如果没有看到代码,几乎不可能找出问题所在......

    听起来您在 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;
    }
    

    【讨论】:

    • 那不行,因为你还没有初始化标签。你需要[[UILabel alloc] initWithFrame:someFrame]。然后,您可以设置其text 属性。
    【解决方案4】:

    3.1.3 不喜欢 [UIColor clearColor];尝试使用与 tableview 相同的背景颜色

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-02
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        • 2012-10-07
        相关资源
        最近更新 更多