这是我的代码

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 61.0
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerCell = tableView.dequeueReusableCellWithIdentifier("CustomHeaderCell") as! CustomHeaderCell
    headerCell.titleLabel.text = "user data"
    return headerCell

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 2
}

不幸的是,自定义标题视图没有显示。

但是,当我只使用这个:
  func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "user data"
}

部分标题存在,我的自定义标题视图代码有什么问题?

最佳答案

不好的是,尽管它们属于tableViewDelegate,但我还是盲目地将它们放到了dataSource中

使它起作用:

extension  UserProfileDelegate: UITableViewDelegate {

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerCell = tableView.dequeueReusableCellWithIdentifier("CustomHeaderCell") as! CustomHeaderCell
    headerCell.titleLabel.text = "user data"
    return headerCell

}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 61.0
}

}

关于ios - Swift-该节的标题不显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38999267/

10-12 14:38
查看更多