问题描述
我已经阅读了关于 SO 的类似问题和解决方案.但似乎没有一个能解决我的问题.我正在使用自定义搜索控制器和自定义搜索栏,并且 func updateSearchResults(for searchController: UISearchController) 没有被调用.
I have read similar problems and solutions on SO. But none seems to solve my problem. I am using Custom Search Controller and Custom Search Bar and func updateSearchResults(for searchController: UISearchController) is not getting called.
var customSearchController: CustomSearchViewController!
CustomSearchViewController:在 ViewDidLoad()
CustomSearchViewController: In 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(_ searchBar: UISearchBar, textDidChange searchText: String) - UISearchBarDelegate 委托方法.
After struggling for hours, I was able to solve it by using:func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) - UISearchBarDelegate delegate method.
代替 updateSearchResults() - UISearchResultsUpdating 委托方法
instead of updateSearchResults() - UISearchResultsUpdating delegate method
希望它对某人有所帮助:)
Hope it helps someone :)
这篇关于updateSearchResults() 没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!