选择searchBar时,在UISearchController中预加载数据

时间:2014-09-30 15:36:50

标签: ios objective-c ios8 uisearchcontroller

在我的应用程序中,我使用UISearchController(iOS 8中的新增功能)来显示搜索结果。 有没有办法显示一个默认的结果集,当用户点击searchBar而不输入任何内容时会出现这些结果? 我找到了一些关于这个主题的问题和答案,但他们都使用了旧的UISearchDisplayController,我似乎无法使用UISearchController解决方案。

由于

2 个答案:

答案 0 :(得分:0)

self.searchController.searchBar.text = @"\n";
似乎要做的伎俩。哈基 - 但是,嘿,好像我们没有其他很多东西可以合作。

答案 1 :(得分:0)

  

我认为这种方法更好,当searchBar为空时要小心,然后preload tableview会再次消失。

UISearchBarDelegate

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        dispatch_async(dispatch_get_main_queue()) {
            self.searchController.searchResultsController?.view.hidden = false
        }
    }
}

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    dispatch_async(dispatch_get_main_queue()) {
        self.searchController.searchResultsController?.view.hidden = false
    }
}

UISearchControllerDelegate

func willPresentSearchController(searchController: UISearchController) {
    dispatch_async(dispatch_get_main_queue()) {
        self.searchController.searchResultsController?.view.hidden = false
    }
}