我已经在SO上阅读了类似的问题和解决方案。但似乎没有一个能解决我的问题。我正在使用自定义搜索控制器和自定义搜索栏,并且没有调用func updateSearchResults(对于searchController:UISearchController)。
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(_ searchBar:UISearchBar,textDidChange searchText:String) - UISearchBarDelegate委托方法来解决它。
而不是updateSearchResults() - UISearchResultsUpdating委托方法
希望它可以帮助别人:)
我不得不在类范围内声明UISearchController
实例。看到这个答案。 https://stackoverflow.com/a/46781890/1511978
以前我在viewDidLoad
方法中声明了它
看起来你没有设置这个:
searchController.searchResultsUpdater = self
确保这是避免出错的最后一个命令。至少这对我有用。
但也许你的问题是你称之为:
navigationItem.titleView = searchController.searchBar
相反,你应该这样做:
navigationItem.searchController = searchController