2016-10-28 128 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] 
} 

我設置背景顏色爲白色,但沒有任何變化。 UITableView中的

回答

2

使用viewForHeaderInSection方法和創建的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

你應該設置的UITableView – iParesh

+0

的heightForHeaderInSection添加了這個功能,還是沒有效果 – 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

在斯威夫特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

斯威夫特3 - 什麼做的把戲對我來說是設置標題的內容查看的的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 
    } 

希望能幫助別人。