如何在TableView中为标题设置背景颜色?
问题描述:
我发现了类似的问题,但没有人帮助我。 我需要设置标题的背景颜色在我的表为白色,它是灰色的,现在(与表格的背景颜色)。如何在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中的
答
使用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
}
答
斯威夫特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
}
答
在斯威夫特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
}
答
斯威夫特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
}
希望能帮助别人。
我代替我的'viewForHeaderInSection'与你(我的输入错误,顺便说一句),但它并没有帮助。 – Jamil
你应该设置的UITableView – iParesh
的heightForHeaderInSection添加了这个功能,还是没有效果 – Jamil