updateSearchResults()没有被调用
问题描述:
我读过类似的问题和解决方案。但似乎没有解决我的问题。我正在使用自定义搜索控制器和自定义搜索栏和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()
}
答
挣扎小时后,我能够通过使用来解决它: FUNC搜索栏(_ searchBar:UISearchBar,textDidChange searchText:String) - UISearchBarDelegate委托方法。
而不是updateSearchResults() - UISearchResultsUpdating委托方法
希望它可以帮助某人:)