Hide searchBar on segmentedIndex is 1

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

@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()

}
By : Peter Pik


Answers

you Try This Code

@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
       tableView.reloadData()

    default:
        break
    }
    tableView.reloadData()

}


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()

}
By : codester


This video can help you solving your question :)
By: admin