Question: I have a UITableView where the searchBar is in the tableViews HeaderView, When the segment is = 1 i want to remove this, but when i try to hide it the searchBar disappears, but the cell does not move to the top. How can i solve this. Here is an image:

![enter image description here](https://i.stack.imgur.com/IlDV1.png)

```
@IBAction func segmentAction(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 0:
        tableView.scrollEnabled = true
        segment = 0
        self.tableView.tableHeaderView?.hidden = false
    case 1:
        tableView.scrollEnabled = false
        segment = 1
        self.tableView.tableHeaderView?.hidden = true

    default:
        break
    }
    tableView.reloadData()

}

```

Answer: 
try this code. its work for me

```
@IBAction func segmentAction(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
    tableView.scrollEnabled = true
    segment = 0
    self.tableView.tableHeaderView = nil
case 1:
    tableView.scrollEnabled = false
    segment = 1
    self.tableView.tableHeaderView = searchBar

default:
    break
}
tableView.setNeedsDisplay()

```

}
You should resize tableView header also try below code.

```
@IBAction func segmentAction(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 0:
        tableView.scrollEnabled = true
        segment = 0
        self.tableView.tableHeaderView!.hidden = false

        var newFrame =  self.tableView.tableHeaderView!.frame;
        newFrame.size.height = 44.0;  //defaut height whatever you want
        self.tableView.tableHeaderView!.frame = newFrame;
    case 1:
        tableView.scrollEnabled = false
        segment = 1
        self.tableView.tableHeaderView!.hidden = true

        var newFrame =  self.tableView.tableHeaderView!.frame;
        newFrame.size.height = 0.0;  //defaut height whatever you want
        self.tableView.tableHeaderView!.frame = newFrame;

    default:
        break
    }
    tableView.reloadData()

}

```