Custom scope buttons

£可爱£侵袭症+ 提交于 2020-01-07 05:25:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!