Search code examples
iosuitableviewtableheader

When adding nil to tableHeaderView cant show it again


I have added table Header in the TableView in Storyboard, I want to hide/show this header.

I have a segmented control, and want the header to show in one of the segments.

To hide the header in one segment I used: tableView.tableHeaderView = nil and this hides it. In other segments, to unhide the header I used:

if tableView.tableHeaderView == nil {
    tableView.tableHeaderView = self.tableViewHeader
}

but the header does not shown again. How to solve this?


Solution

  • If you set

    tableView.tableHeaderView == nil
    

    Than You create a local variable to store tableView.tableHeaderView on viewDidLoad as this

    self.tableViewHeader = tableView.tableHeaderView
    

    than check

    if tableView.tableHeaderView == nil 
    {
     tableView.tableHeaderView = self.tableViewHeader
    }