theDC
theDC

Reputation: 6484

Swift - header for section does not show

This is my code

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
}

Unfortunately, custom header view does not show up.

However, when I use just this:

  func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "user data"
}

section headers are present, what is wrong with my custom header view code?

Upvotes: 2

Views: 2251

Answers (1)

theDC
theDC

Reputation: 6484

My bad, I blindly put these functions into dataSource although they belong to tableViewDelegate

to make it work:

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
}

}

Upvotes: 3

Related Questions