在我的代码中,我使用下面的代码来设置tableview的节标题。它运作良好。
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
//return self.adHeaders[section]
return self.headers[section]
}
我想自定义标题的背景颜色,所以我在上面的代码之前插入下面的代码。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor(red: 232/255.0, green: 237/255.0, blue: 242/255.0, alpha: 1.0)
return headerView
}
结果,背景颜色改变,而标题文本丢失。
当你实现viewForHeaderInSection
时,根本不会调用titleForHeaderInSection
。因此,您需要在viewForHeaderInSection
中设置标题视图的标题。您可能需要创建UILabel。
你还必须实现heightForHeaderInSection
。