Makaveli
Makaveli

Reputation: 309

tableHeaderView in UIViewController

i would like to use a tableview in my UIViewController, and I want to use "tableHeaderView" but i believe, by default, tableHeaderView is "nil"

is there a way to initialize it ? or in other words, how to use "tableHeaderView" in UIViewController?

Thanks

Upvotes: 0

Views: 2828

Answers (2)

tnek316
tnek316

Reputation: 640

If you want to make a header for the entire tableview then you would want to do something like this

override func viewDidLoad() {
    super.viewDidLoad()
    let header  = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 10))
    header.backgroundColor = UIColor.blackColor()
    tableView.tableHeaderView = header
}

or if you want to do headers within a section of the tableview you would do something like this

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //give height for header
    return 10
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //you want to provide your view here
    return nil
}

Upvotes: 4

Jonah
Jonah

Reputation: 17958

tableHeaderView is a settable property of UITableView. Construct the view you want to use as the header and set it as the header of your table view.

Upvotes: 0

Related Questions