Search code examples
swiftuitableviewcellstableheader

Make a TableHeaderView not scrolling


I made a TableView, with prototypes cells; in first position of the tableView I made this

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0
    {
        let view : UIView = UIView(frame: CGRectMake(0, 0, 400, 140))
        view.backgroundColor = UIColor.redColor()
        view.addSubview(populateView())
        //populateView() is a method by which I programmatically made all object in the view (label, images, textView)
        return view
    }
    else
    {
        return nil
    }
}

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
    if section == 0
    {
        return 140
    } else
    {
        return 0
    }
}

and the result is this (it's a test): screenshot

Now I'd like to make the TableHeaderView not scrolling with the tableView; I made tableView style as plain; and I found this Change Default Scrolling Behavior of UITableView Section Header and this https://corecocoa.wordpress.com/2011/09/17/how-to-disable-floating-header-in-uitableview/ but I don't know Objective-c (I am swift programmer from just 6 months). can anyone solve my problem?


Solution

  • I would definitely use UIViewController with UITableView and custom view above it (see below).

    enter image description here

    Or if you want to use UITableViewController (which brings some extra features), you can embed it under the header using container view.

    enter image description here