-1

My target is to change the UITableView section header height at runtime. Let me be more specific. Default height is 44 but on scroll before touching the top, height will be 64. I have created a subclass of UITableViewHeaderFooterView and it's uses autolayout.

I tried

var frame = sectionHeader.frame
frame.size.height = 64
sectionHeader.frame = frame

and also

tableView.sectionHeaderHeight = 64

but nothing work for me. Can anyone put some light on this problem.

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86

3 Answers3

1
  1. when using autolayout change frame directly won't work
  2. to change tableview section header you need implement delegate method and reload data after change

optional func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

dopcn
  • 4,218
  • 3
  • 23
  • 32
1

You don't need to subclass the footer to change I'ts height.

  1. Check you are only setting I'ts height at: (no delegate methods)

tableView.sectionHeaderHeight = 64

  1. Check you call or "reloadData()" on the tableView after. or

[UIView setAnimationsEnabled:NO];

[tableView beginUpdates];

[tableView endUpdates];

[UIView setAnimationsEnabled:YES];

To only change height without reloading the tableView.

MCMatan
  • 8,623
  • 6
  • 46
  • 85
0

I think you have to reload the entire section of the table view, since there is no public API for only updating the section header.

self.isOnTop = true // or false
tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)

And change a property that modifies the return value from the aproppriate delegate method:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    isOnTop ? 44 : 64
}

You can also try the following methods, that can update the layout without reloading:

tableView.beginUpdates()
tableView.endUpdates()

Or move to a collection view solution, that supports the tableview's behaviour: https://github.com/jamztang/CSStickyHeaderFlowLayout

Kádi
  • 2,796
  • 1
  • 16
  • 22