Подтвердить что ты не робот

Есть ли способ установить цвет заголовка раздела UITableView в iOS 7 напрямую, а не создать новый UIView

Заголовки заголовков UITableView менее идентифицируются в iOS 7. Есть ли способ установить цвет заголовка раздела UITableView в iOS 7 напрямую, а не создать новый UIView.

enter image description here

Примечание. Я нашел некоторые решения, создав новый UIView in,

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

но я действительно хотел сохранить свойства Apple, кроме цвета. Есть ли способ сделать это без этого метода.

4b9b3361

Ответ 1

Внесите tableView:willDisplayHeaderView:forSection: и обновите представление, которое вы передали.

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

(предположим, что вы предоставили экземпляр UITableViewHeaderFooterView в своей реализации для tableView:viewForHeaderInSection:)

Ответ 2

В случае Swift 3 способ заключается в достижении этого с помощью метода UITableViewDelegate:

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

Ответ 3

С помощью этого метода вы можете установить размер шрифта, стиль шрифта и фон заголовка.

Для этого есть 2 метода:

Первый метод

- (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;
}