Reputation: 115
So my goal is to have my first tableview headerview stay visible after scroll down. So I have a tableview with an inset grouped
style so that the headers move up with the cells, when the view loads, the first header label is visible like so:
This is great. I love it. Now the issue is when I begin to scroll down in the tableview and the cell disappears out of the view, when I scroll back up the cell header is gone:
The weird thing about this is that there's three sections with cells and the last section is out of the view when the view loads and requires the user to scroll down, but when I scroll down to it, scroll back up where it's not visible, and scroll down again, the header view is still there which I don't understand. Why does the first section header not have the same behaviour as the last section header?
Here is my code for the configuration of my headers:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Account Settings"
} else if section == 3 {
return "Support & Info"
} else if section == 6 {
return "Notification Settings"
} else {
return "label"
}
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
if section == 0 || section == 3 || section == 6 {
header.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont, size: 14.0)
} else {
header.textLabel?.alpha = 0
}
header.textLabel?.textAlignment = NSTextAlignment.left
header.backgroundConfiguration = .clear()
}
Any suggestions?
Upvotes: 1
Views: 613
Reputation: 26036
Headers are reused.
When you do something in a if
, you need to "reset the value" in the else
.
Well, in your case you are doing it in the else
(.alpha = 0
), but you see the idea.
So:
if section == 0 || section == 3 || section == 6 {
header.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont, size: 14.0)
} else {
header.textLabel?.alpha = 0
}
Should be:
if section == 0 || section == 3 || section == 6 {
header.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont, size: 14.0)
header.textLabel?.alpha = 1
} else {
header.textLabel?.alpha = 0
}
Upvotes: 0