0

Width of my headerView does not adapt to the width of the screen. My viewForHeaderInSection function:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("sectionCell") as! sectionCell
    cell.hint.text = "some name"

    cell.tag = section


    let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapOnSection(_:)))

    cell.addGestureRecognizer(singleTap)

    let view = UIView(frame: cell.frame)

    view.addConstraints(cell.constraints)

    view.addSubview(cell)

    return view
}

I must add cell to the UIView (or something similar) because i have hiding of row in the section. In my opinion i must add to "view" some constraints programmatically, but i dont know how

farzadshbfn
  • 2,710
  • 1
  • 18
  • 38
Przemek Bartnik
  • 37
  • 1
  • 10
  • I answered another approach to handle things here, but i think your problem is; you're adding `cell` as subview of the view, but you have to add `cell.contentView` instead. and also instead of `cell.constraints`, use `cell.contentView.constraints` – farzadshbfn Apr 18 '16 at 18:19

2 Answers2

0

You should define headerview with same width of tableview and height what you require. don't give cell's height or width. If you are using storyborard for ui then drag uiview on tableview at upper side it will automatically set as headerview and then give top,leading,trailing and fix heigth constraints and remove viewForHeaderInSection method.

hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • this work, but i need viewForHeaderInSection because i have many dynamically created sections in one table view with different texts, and i need add UITapGestureRecognizer to some header witch will expand or collapse this section – Przemek Bartnik Apr 19 '16 at 11:23
0

Define a customView: UITableViewHeaderFooterView class, and in viewDidLoad: of View Controller write

tableView.registerClass(customView.classForCoder(), forHeaderFooterViewReuseIdentifier: "HeaderIdentifier")

And instead of dequeueing a cell, dequeue a UITableViewHeaderFooterView (as it is the right way to do it).

This way you have to add auto layout in code or define it in a nib file, working with nibs and handling all kinds of things is kinda frustrating, if you're using this approach i suggest use this library NibDesignable.

farzadshbfn
  • 2,710
  • 1
  • 18
  • 38
  • This way is described on [this page](http://samwize.com/2015/11/06/guide-to-customizing-uitableview-section-header-footer/) but this give me a few warnings and errors, and i can't make this to work – Przemek Bartnik Apr 19 '16 at 11:26
  • "Could not load NIB in bundle" and nothing what can explain what is the problem – Przemek Bartnik Apr 19 '16 at 11:46
  • Did you look at [NibDesignable](https://github.com/mbogh/NibDesignable)? It's really easy to use and you don't have to worry about the nib loading and stuff anymore. – farzadshbfn Apr 19 '16 at 11:57
  • For working with `NibDesignable` and reusableCells and reusableViews check [here](http://stackoverflow.com/q/34692590/3857417) too. – farzadshbfn Apr 19 '16 at 11:59