如何在TableView中设置标题的背景颜色?

Jam*_*mil 2 uitableview swift

我发现了类似的问题,但没有一个对我有帮助。我需要将表格标题中的背景颜色设置为白色,现在为灰色(与表格的背景颜色相同)。

这是我的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]
}
Run Code Online (Sandbox Code Playgroud)

我将背景色设置为白色,但是没有任何变化。

iPa*_*esh 6

viewForHeaderInSectionUItableview的使用方法和创建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
}
Run Code Online (Sandbox Code Playgroud)


小智 5

Swift 3-对我来说,窍门是设置标题的contentView的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
    }
Run Code Online (Sandbox Code Playgroud)

希望能对某人有所帮助。