有没有办法直接在iOS 7中设置UITableView节标题颜色,而不是创建新的UIView

有没有办法直接在iOS 7中设置UITableView节标题颜色,而不是创建新的UIView,uitableview,ios7,Uitableview,Ios7,iTableView的节标题在iOS 7中不易识别。有没有办法直接在iOS 7中设置UITableView节标题颜色,而不是创建新的UIView 注意:我通过在中创建新的UIView找到了一些解决方案 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 但我真的很想保留苹果的特性,除了颜色。没有此方法,是否有任何方法可以执行此操作。实现表视图:willDisplayH

iTableView的节标题在iOS 7中不易识别。有没有办法直接在iOS 7中设置UITableView节标题颜色,而不是创建新的UIView

注意:我通过在中创建新的UIView找到了一些解决方案

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

但我真的很想保留苹果的特性,除了颜色。没有此方法,是否有任何方法可以执行此操作。

实现
表视图:willDisplayHeaderView:forSection:
并更新传递给您的视图

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *v = (UITableViewHeaderFooterView *)view;
    v.backgroundView.backgroundColor = [UIColor darkGrayColor];
}

(假设您在
tableView:viewForHeaderInSection:
的实现中提供了
UITableViewHeaderFooterView
实例)

使用此方法,您还可以设置字体大小、字体样式和标题背景

为此,有两种方法:

第一种方法

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
        UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
        header.backgroundView.backgroundColor = [UIColor darkGrayColor];
        header.textLabel.font=[UIFont fontWithName:@"Open Sans-Regular" size:12];
        [header.textLabel setTextColor:[UIColor whiteColor]];
    }
第二种方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
//    myLabel.frame = CGRectMake(20, 8, 320, 20);
    myLabel.font = [UIFont fontWithName:@"Open Sans-Regular" size:12];
    myLabel.text = [NSString stringWithFormat:@"   %@",[self tableView:FilterSearchTable titleForHeaderInSection:section]];

    myLabel.backgroundColor=[UIColor blueColor];
    myLabel.textColor=[UIColor whiteColor];
    UIView *headerView = [[UIView alloc] init];
    [headerView addSubview:myLabel];
    return headerView;
}

对于Swift 3,方法是通过UITableViewDelegate方法实现:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView  {
        headerView.backgroundColor = UIColor.gray
    }
}

@AlexStone,感谢您的编辑,但是要小心将
UIView*
转换为子类(特别是在回答问题时;-))您不需要实现
tableView:ViewForHeaderSection:
,正如您的回答所说。
tintColor=[UIColor whiteColor]
不起作用(给出了灰色标题),只有这个有帮助。应该是:headerView.backgroundView.backgroundColor=.gray