2016-10-28 3 views
1

Я нашел похожие вопросы, но ни один из них не помог мне. Мне нужно установить цвет фона заголовков в моем столе на белый, теперь он серый (тот же, что и цвет фона в таблице).Как установить цвет фона для заголовка в TableView?

Вот мои tableView функции:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{ 
    let header = view as! UITableViewHeaderFooterView 
    header.backgroundColor = UIColor.white 
    header.textLabel?.font = UIFont(name: "Futura", size: 13)! 
    header.textLabel?.textColor = UIColor.blue 
} 

func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? { 
    tableView.backgroundColor = UIColor.white 
    return tableView 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
{ 
    return titles[section] 
} 

Я устанавливаю белый цвет фона, но ничего не меняется.

ответ

2

Применение viewForHeaderInSection метод UITableView и UIView Создать

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerView = UIView() 
    headerView.backgroundColor = UIColor.white 
    return headerView 
} 
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 15 
} 
+0

Я заменил свой 'viewForHeaderInSection' вашим (мой был опечален), но это не помогло. – Jamil

+0

Вы должны установить heightForHeaderInSection UITableview – iParesh

+0

добавили эту функцию, все равно никакого эффекта – Jamil

0

Свифта 3 ниже. Одна заметная разница с предыдущим ответом, не забудьте ключевое слово override. Если вы это сделаете, это не будет отменять функцию и не будет работать.

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let headerView = UIView() 
     headerView.backgroundColor = UIColor.darkGray 
     return headerView 
    } 
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 15 
    } 
2

В Swift 3, это работает для меня:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let header = view as! UITableViewHeaderFooterView 
    header.textLabel?.font = UIFont.(name: "Futura", size: 13)! 
    header.backgroundView?.backgroundColor = UIColor.white 
} 
2

Swift 3 - Что сделал трюк для меня была установка contentView «s BackgroundColor заголовке в.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
      let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header") 
      header.contentView.backgroundColor = .blueSteel 
      return header 
    } 

Надежды, которые помогают кому-то.

Смежные вопросы