2009-06-03 188 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!