Day of Swift: Disable sticky Table header
Sep 2, 2018 · 1 min read
As title, there are several ways to do that.
Set the UITableView style as grouped
let tableview = UITableView(frame: CGRect.zero,
style: .grouped)Custom UITableView contentInset
let tableHeaderHeight = CGFloat(40)
let tableWidth = self.tableView.bounds.size.width
let tableFrame = CGRect(x: 0, y: 0, width: tableWidth , height: tableHeaderHeight)
self.tableView.tableHeaderView = UIView(frame: tableFrame)// set the content insets top value
self.tableView.contentInset = UIEdgeInsetsMake(-tableHeaderHeight, 0, 0, 0)
It sets the tableview contentInset top as navigate value. The table header will still sticky on the top but the user won’t see that due to the space in content inset top is set same as header height.
Use UITableViewCell as Header
The last way is using cellForRow indexPath:.
But this way will increase the complexity of your data source management when you need to reload specified row or update one of the cell data.