2012-08-29 120 views
0

我想改變字體顏色在我的頭,所以,我委派viewForHeaderInSection:顏色更改爲標題中的UITableView

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)]; 

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)]; 

    [headerLabel setBackgroundColor:[UIColor whiteColor]]; 
    [headerLabel setTextColor:[UIColor blackColor]]; 
    [headerView addSubview:headerLabel]; 

    return headerView; 
} 

的背景顏色設置,但文本喜歡透明, 什麼也沒有。

我試着評論backgroundColor白色消失,但文字沒有。

我試着評論所有的塊,文本以默認的顏色出現。

我的錯誤在哪裏?謝謝你在前進

我找到了,我錯過了:

[headerLabel setText: @"myText"]; 

看來:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section 

不叫執行viewForHeaderInSection時。

謝謝!

回答

0

希望從的UITableViewDelegate協議將得到這個方法,你開始:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease]; 
if (section == integerRepresentingYourSectionOfInterest) 
[headerView setBackgroundColor:[UIColor redColor]]; 
    else 
[headerView setBackgroundColor:[UIColor clearColor]]; 
    return headerView; 
} 

更換[的UIColor redColor]與兩者的UIColor你想。你也可能希望調整headerView的尺寸。

+0

謝謝,我需要的字體顏色,但我已經得到它!我編輯了我的帖子。謝謝! – user1256477

+0

我在你的代碼上面測試了你的代碼 –