Swift 如何在TableView中设置标题的背景色?
我发现了类似的问题,但没有一个对我有帮助。 我需要将表格标题的背景色设置为白色,现在是灰色(与表格的背景色相同) 以下是我的Swift 如何在TableView中设置标题的背景色?,swift,uitableview,Swift,Uitableview,我发现了类似的问题,但没有一个对我有帮助。 我需要将表格标题的背景色设置为白色,现在是灰色(与表格的背景色相同) 以下是我的tableView功能: func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header = view as! UITableViewHeaderFooterView header.backgr
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的
视图for headerinSection
方法并创建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
}
下面是Swift 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
}
在Swift 3中,这对我很有用:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel?.font = UIFont(name: "Futura", size: 13)!
headerView.backgroundView?.backgroundColor = .white
}
Swift 3-我的诀窍是设置标题的内容视图的背景色
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
}
希望这对其他人有所帮助。我用你的视图替换了我的
headerinsection
(顺便说一句,我的视图输入错误),但没有帮助。你应该设置uitableview的heightForHeaderInSection添加了此功能,仍然没有effect@Jamil选中Tableview样式,如果已分组,则必须在情节提要中将其更改为普通样式是的,已分组。我需要表格各部分之间的距离,是否可以使用平原?谢谢,这实际上是唯一一个不使用自定义视图的答案。是的,它正在工作。谢谢