我有一个使用Swift3和TableView构建的应用程序。我使用故事板,tableview放置在视图控制器内。我正在通过代码使用部分和设置部分标题。我希望节标题显示与我为表视图设置的相同的灰色背景。我试图设置相同的颜色代码,但它没有帮助。所以我尝试设置为UIColor.clear,但它显示为部分标题的白色背景。我需要它使用tableview背景。你能指导我什么是错的。UIColor.Clear在swift3中使用部分标题时没有给出预期结果
代码:
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
print("willDisplayFooterView.. for section \(section) ")
(view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0)
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
print("willDisplayHeaderView,... for section \(section) ")
(view as! UITableViewHeaderFooterView).backgroundView?.backgroundColor = UIColor.clear //UIColor(red: 232.0, green: 232.0, blue: 232, alpha: 1.0)
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
print("titleForHeaderInSection")
return self.section [section ]
}
我试图用viewForHeaderInSection,但它不会被调用。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print("in viewForHeaderInSection")
let view = UIView()
let label = UILabel()
label.text = self.section [section ]
view.addSubview(label)
view.backgroundColor = UIColor.clear
label.translatesAutoresizingMaskIntoConstraints = false
let horizontallayoutContraints = NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0)
view.addConstraint(horizontallayoutContraints)
let verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
view.addConstraint(verticalLayoutContraint)
return view
}
您好..它仍然不给我预期的行为。我试着按照你的建议使用,并警告说这是一个不赞成的行为,而不是使用contentView背景色。所以我尝试了使用它,但仍然没有运气。 func tableView(_ tableView:UITableView,willDisplayFooterView view:UIView,forSection section:Int){ print(“willDisplayFooterView .. for section \(section)”) (view as!UITableViewHeaderFooterView).contentView.backgroundColor = UIColor.clear } – csharpnewbie
你是否重写这个方法? “func tableView(tableView:UITableView,viewForHeaderInSection section:Int) - > UIView?” –
嗨..因为我在原始问题中编辑过,我尝试使用viewForHeaderInSection,但它根本没有被调用。 – csharpnewbie