问题
I'm trying to create custom scope buttons, but i'm having trouble actually linking them to my buttons. This is what I have
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] )
}
}
This is the array that I was using to pass the category in which to search for from my function
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()
}
来源:https://stackoverflow.com/questions/43153579/custom-scope-buttons