1

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?

Community
  • 1
  • 1
Fabio Cenni
  • 841
  • 3
  • 16
  • 30

2 Answers2

3

The solution is to use a UIViewController, rather than a UITableViewController. A table view controller will claim the whole view and there is no space above.

Instead, you have to use a UIViewController, add a subview as the header, and add a UITableView also as a subview. You populate and interact with it by explicitly including the data source and delegate protocols.

class HeaderTableViewController : UIViewController, 
                                  UITableViewDataSource, UITableViewDelegate
Mundi
  • 79,884
  • 17
  • 117
  • 140
3

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

Pavel Smejkal
  • 3,600
  • 6
  • 27
  • 45
  • 1
    Well maybe the first option is more simple. But if you want to use UITableViewControllers, then you can basically pass the information to the tableViewController when loaded – using either "self.childViewControllers.first" or get pointer to the embedded UITableViewController in the "performSegue:" method. (embedding controllers is just a special kind of segue) – Pavel Smejkal Aug 01 '15 at 20:56
  • I fail to see how this answer improves on the one I gave 2 hours earlier. – Mundi Aug 01 '15 at 23:05