我正在尝试创建自定义范围按钮,但我实际上无法将它们链接到我的按钮。这就是我所拥有的
extension HomeViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = titles[searchBar.selectedScopeButtonIndex]
filterContentForSearchText(searchText: searchController.searchBar.text!, scope: scope)
}
}
extension HomeViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchText: searchBar.text!, scope: titles[selectedScope] )
}
}
这是我用来传递从我的函数
中搜索的类别的数组 var titles = [String]()
func firstFunc(){
titles = ["Pizza"]
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredCandies = candies.filter { candy in
let categoryMatch = (scope == "All")||(candy.Food == scope)
return categoryMatch && candy.Calories.lowercased().contains(searchText.lowercased())
}
tableView.reloadData()
}