如何使用ios 8 UISearchController单击搜索栏时隐藏节标题?

时间:2015-06-07 01:06:56

标签: ios8 uisearchcontroller

enter image description here 这是之前的屏幕。

然后,我点击了搜索栏: enter image description here

代码如下:     // 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
    })()
}

请帮帮我。感谢。

1 个答案:

答案 0 :(得分:2)

将该部分的标题设置为nil,如果该部分中没有行,则会隐藏部分标题

// 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)"
    }
}

这将有效,假设tableView(tableView: UITableView, numberOfRowsInSection section: Int)已正确实施,以反映基于搜索结果的部分数量。

如果要在搜索栏处于活动状态时完全删除部分标题,请添加此

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