2012-08-23 80 views
1

我想顯示帶自定義標題標題的表。 table view附加到controller class,該controller class實現了tableview delegate和數據源協議,但不是UIViewController的子類,因爲該表是子視圖,要顯示在另一個tableview上方。UITableView:自定義標題標題視圖不顯示

我的一些代碼片段: 的實現代碼如下的編程方式創建:

_myListView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped]; 

[_myListView setDataSource:self.myListController]; 
[_myListView setDelegate:self.myListController]; 
[_myListView setBackgroundColor:darkBackgroundColor]; 

其中myListController是班上一個強大的屬性。

對於行中部分的數量:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    … 
    return count;  
} 

區段的數目:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [someDelegate sectionCount]; 
} 

對於自定義首部視圖:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)]; 
    UILabel* sectionHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, 300, 24)]; 

    [headerView setBackgroundColor:[UIColor clearColor]]; 

    sectionHeaderTitle.text = [self myTitleForHeaderInSection:section]; 
    sectionHeaderTitle.textColor = [UIColor whiteColor]; 
    sectionHeaderTitle.textAlignment = UITextAlignmentLeft; 

    [headerView addSubview:sectionHeaderTitle]; 

    return headerView; 
} 

對於自定義headerViewHeight(如自iOS5以來需要):

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    if ([self tableView:tableView numberOfRowsInSection:section] > 0) { 
     return SectionHeaderHeight; 
    } else { 
     return 0; 
    } 
} 

不幸的是,tableview不顯示任何節標題,就像我會返回零。 但是,我已經檢查了一個斷點,該代碼實際上返回一個UIView。

其他一切正常。

我錯過了什麼?請放心,不要猶豫,讓我爲自己感到羞愧。

+0

你爲什麼不能設置背景顏色爲你的「headerView」和檢查,它是可見的?編輯:*也爲'sectionHeaderTitle'標籤... – Nina

+0

已經做到了,嘗試redColor都沒有成功 –

+0

我試過你的'viewForHeaderInSection'在我的項目。它對我來說工作得很好。 – Nina

回答

0

我似乎已經找到了解決方案:

我已經創建了每個標題視圖我想顯示遲緩裝載強屬性。 (幸運的是隻有三個)

現在顯示視圖。

似乎在表格呈現之前,標題視圖沒有強引用而被釋放。

難道是有一個實現表視圖的類的連接委託和數據源協議不是UIViewController?

1

我不明白你爲什麼要使用自定義視圖,而不是「標準」視圖?你可以有你的理由,但我沒有看到你的代碼什麼告訴我爲什麼:)

我個人只是用這樣的:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    if (section == 0) return @"First section header title"; 
    if (section == 1) return @"Second section header title"; 
    else return nil; 
} 

告訴我,如果這就是你要找沒有什麼!

+0

好的,我可能錯過了第一行,說:「我想顯示一個自定義標題標題的表格。」。如果我完全偏離主題,我很抱歉:) – rdurand

+0

我想要顯示自定義標題的主要原因是在表視圖中黑暗的背景,並且我想在標題中顯示一個圖標。但我不介意偏離主題的答案;) –

+0

'heightForHeaderInSection'是否返回正確的值?你用斷點檢查過嗎? – rdurand

0

將文本顏色更改爲黑色並檢查一次。

sectionHeaderTitle.textColor = [UIColor blackColor];

+0

桌面視圖背景對於黑色文本顏色已經很暗,但我嘗試了藍色,但仍然沒有標題可見。 –

+0

我試過了你提供的任何代碼。只是改變了文字顏色,它對我來說工作得很好。 –

+0

嘗試將heightForHeaderInSection設置爲40或任何有效值。 –

0

你試試這個代碼這項工作在我身邊:-)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 24)]; 

    UIImage *myImage = [UIImage imageNamed:@"SectionBackGround.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage]; 
    imageView.frame = CGRectMake(0,0,320,24); 

    UIImage *imageIcon = [UIImage imageNamed:@"SectionBackGround.png"]; 
    UIImageView *iconView = [[UIImageView alloc] initWithImage:myImage]; 
    iconView.frame = CGRectMake(0,0,320,24); 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 24)]; 
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont boldSystemFontOfSize:14]; 


    [view addSubview:imageView]; 
    [view addSubview:iconView]; 
    [view addSubview:label]; 
    return view; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 24; 
}