How to set scope in UISearchBar
I have a UISearchBar which is working fine, but I was wondering how I can set a scope and filter results based on certain criteria l开发者_JS百科ike: "Dogs and "Cats"
Tutorials and example code welcome.
If you use Interface Builder just add the scope button titles. To add them programmatically, see this answer.
The scope buttons are indexed from 0.
Implement UISearchDisplayDelegate
and UISearchBarDelegate
. You will have to implement the method below (along with 2 others)
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
Inside the method above use searchOption
to get the scope index the user has selected.
If your criteria has to be in string form then you should find a way to map the string to an integer value e.g use the NSDictionary
or just manually compare if (0 == searchOption) ...
精彩评论