I'm trying to change the height of the header view as the user scrolls following this tutorial . However, when I try to access self.songsTable.headerView(forSection: 0)?.frame.size.height
(so I can update the height of the header view), the value is nil and the debugger tells me "header view has no height". I created my header view programmatically using heightForHeaderInSection
and viewForHeaderInSection
.
Code
class SongsViewController {
var currentHeaderHeight: CGFloat = 136
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Throws error
guard let currentHeaderHeight = self.songsTable.headerView(forSection: 0)?.frame.size.height else {
print("header view has no height")
return
}
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return currentHeaderHeight
}
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.frame = CGRect(x: 0, y: 0, width: Screen.width, height: maxHeaderHeight)
return headerView
}
}
This may just be missing in your code snippet, but have you set up your viewController as the TableViewDataSource?
class SongsViewController: UIViewController, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
songsTable.dataSource = self
}
// ... the rest of your code ...
}
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.