2010-09-22 58 views
1

我使用此代碼設置標題和背景顏色與UITableView但標題不顯示?UITableView中的標題不顯示?

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section 
{ 
    return @"Contents"; 
} 

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease]; 
    [headerView setBackgroundColor:[UIColor brownColor]]; 
    [self.view sendSubviewToBack:headerView]; 

    return headerView; 
} 

請幫助...

回答

1

如果我沒記錯的話,一旦你viewForHeaderInSection返回的東西在titleForHeaderInSection返回的值不再使用。在viewForHeaderInSection添加一個UILabel作爲一個子視圖來headerView和文本設置爲@"Contents"

0

請改變你的代碼,這和ckeck只有你自己能夠實現viewForHeaderInSection方法:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

    NSString *cellValue = @""; 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] ; 
    [headerView setBackgroundColor:[UIColor brownColor]]; 

    UILabel *lblContent = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)]; 
    cellValue = [[arrSections objectAtIndex:0] valueForKey:@"CategoryName"]; 
    lblContent.text = cellValue; 
    [headerView addSubview:lblContent]; 
    return headerView; 
}