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

问题描述 投票:0回答:1

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

结果,背景颜色改变,而标题文本丢失。

ios swift uitableview
1个回答
2
投票

当你实现viewForHeaderInSection时,根本不会调用titleForHeaderInSection。因此,您需要在viewForHeaderInSection中设置标题视图的标题。您可能需要创建UILabel。

你还必须实现heightForHeaderInSection

© www.soinside.com 2019 - 2024. All rights reserved.