如何更改iPhone SDK中Grouped TableView中的Section Headers的文本颜色?

Par*_*att 22 iphone cocoa-touch objective-c uitableview ios4

我正在制作一个iPhone应用程序,其中我有一个分组的TableView与章节的标题.

问题是我想要更改Section Header的文本颜色.

如何更改Section Header的文本颜色?

我该怎么办?

Vah*_*han 53

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中将以下代码添加到AppDelegate类:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)

  • 对于iOS 9+,应该使用`[[UILabel appearanceWhenContainedInInstancesOfClasses:@ [[UITableViewHeaderFooterView class]]] setTextColor:[UIColor whiteColor]];`而不是. (5认同)
  • 将它添加到'tableView:titleForHeaderInSection:',它就像一个魅力! (2认同)

Mat*_*ler 34

如果你不想像Vahan的解决方案那样广泛应用,那么这是一个使用UITableViewDelegate方法之一的解决方案:

func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
       headerView.textLabel?.textColor = UIColor.redColor() 
    }
}
Run Code Online (Sandbox Code Playgroud)


Dev*_*per 33

这是SURELY会为你工作.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor clearColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}
Run Code Online (Sandbox Code Playgroud)

只需在代码中复制并粘贴此功能即可.