How to hide section title when clicking search bar using iOS 8 UISearchController?

enter image description here This is the previous screen.

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

Code 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. Thank.

+3


source to share


1 answer


Set the section title to zero, if there are no lines in this section, this will 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 it tableView(tableView: UITableView, numberOfRowsInSection section: Int)

is running correctly, to reflect the number of sections based on the search result.



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

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

      

+2


source







All Articles