how to hide the section title when clicked search bar using ios 8 UISearchController?

650 views Asked by At

enter image description here This is the previous screen.

Then, I clicked the search bar: enter image description here

Code is below: // MARK: - searchController

func initSearchController() {
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.backgroundColor = UIColor.clearColor()

        self.tableView.tableHeaderView = controller.searchBar
        return controller
    })()
}

Please help me. Thanks.

1

There are 1 answers

1
Praveen Gowda I V On BEST ANSWER

Set the title of the section to nil, if there are no rows in that section, this would hide the section title

// Set the title of the section header, return nil if no rows in that section
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if self.tableView(tableView, numberOfRowsInSection: section) == 0 {
        return nil
    } else {
        return "section title \(section)"
    }
}

This will work, assuming tableView(tableView: UITableView, numberOfRowsInSection section: Int) is implemented properly to reflect the number of sections based on the search result.

If you want to remove the section title altogether when the search bar is active, then just add this

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if self.resultSearchController.active {
        return nil
    } else {
        return "section title \(section)"
    }
}