用到过的方法总结一下,以免遗忘。
拿到数据源之后,筛选所需的数据。数据源的getFriendsArray
为模型数组
/**
* 搜索
*
* @param searchText searchText description
*/
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
self.searchResultArray = (NSMutableArray *)[[self.getFriendsAPIManager.getFriendsArray valueForKey:@"nickname"] filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",self.searchResultArray);
}
在UISearchDisplayController代理里面进行操作
#pragma mark - UISearchDisplayController delegate methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}