简体   繁体   中英

Change UITableViewHeaderFooterView height at runtime

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.

  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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM