2017-01-10 125 views
3

我讀過類似的問題和解決方案。但似乎沒有解決我的問題。我正在使用自定義搜索控制器和自定義搜索欄和func updateSearchResults(對於searchController:UISearchController)沒有被調用。updateSearchResults()沒有被調用

var customSearchController: CustomSearchViewController! 

CustomSearchViewController:在viewDidLoad中()

customSearchController = CustomSearchViewController(searchResultsController: ***nil***, searchBarFrame: CGRect(x: 0.0, y: 0.0, width: searchTableView.frame.size.width, height: 44.0), searchBarFont: UIFont(name: "HelveticaNeue", size: 16.0)!, searchBarTextColor: UIColor.purple, searchBarTintColor: UIColor.white) 

customSearchController.searchResultsUpdater = self 
customSearchController.definesPresentationContext = true 
customSearchController.customSearchBar.placeholder = "What are you looking for?" 
customSearchController.customSearchBar.backgroundColor = UIColor.white 
customSearchController.customSearchBar.sizeToFit() 
customSearchController.customSearchBar.resignFirstResponder() 
customSearchController.customSearchBar.showsCancelButton = true 
customSearchController.customSearchBar.delegate = self 

沒有得到所謂::(

func updateSearchResults(for searchController: UISearchController) { 
    filtered.removeAll() 
    filtered = searchArray.filter({ (text) -> Bool in 
     let tmp: NSString = text as NSString 
     let range = tmp.range(of: customSearchController.customSearchBar.text!, options: NSString.CompareOptions.caseInsensitive) 
     return range.location != NSNotFound 
    }) 
    self.searchTableView.reloadData() 
} 

回答

4

掙扎小時後,我能夠通過使用來解決它: FUNC搜索欄(_ searchBar:UISearchBar,textDidChange searchText:String) - UISearchBarDelegate委託方法。

而不是updateSearchResults() - UISearchResultsUpdating委託方法

希望它可以幫助某人:)

相關問題