Ios 自定义节标题视图时节标题丢失

Ios 自定义节标题视图时节标题丢失,ios,swift,uitableview,Ios,Swift,Uitableview,在我的代码中,我使用下面的代码设置tableview的节标题。而且效果很好 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { //return self.adHeaders[section] return self.headers[section] } 我想自定义标题的背景色,所以在上面的代码之前插入下面的代码 fu

在我的代码中,我使用下面的代码设置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


当您实现
viewForHeaderInSection
时,还必须实现
heightForHeaderInSection
标题forheaderinsection
根本不被调用。因此,您需要在
viewForHeaderInSection
中设置标题视图的标题。您可能需要创建一个UILabel


您还必须执行
heightForHeaderInSection

谢谢。它起作用了。因为我没有太多的IOS开发经验。我只是想知道自定义标题视图是否正确。是的,自定义标题是自定义的方法。谢谢。它起作用了。因为我没有太多的IOS开发经验。我只是想知道这是自定义标题视图的正确方法。是的,自定义标题是自定义的方法。您甚至可以查看,甚至可以查看