如何最佳实践地过滤NSFetchedResultsController
的数据?每次搜索栏文本变化时我需要重新初始化吗?
我正在使用一个UISearchDisplayControllers
,并实现了:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;
感谢。
如何最佳实践地过滤NSFetchedResultsController
的数据?每次搜索栏文本变化时我需要重新初始化吗?
我正在使用一个UISearchDisplayControllers
,并实现了:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;
感谢。
Guy的答案代码与问题有什么不同? 我猜filterContentForSearchText:scope方法是由shouldReload方法调用的?
无论如何,在CoreDataBooks示例中添加了一些类似的代码以包括搜索。 在IB中为CoreDataBooks示例添加一个Search Display Controller。 然后,我在RootViewController.m中添加了以下代码:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
NSInteger searchOption = controller.searchBar.selectedScopeButtonIndex;
return [self searchDisplayController:controller shouldReloadTableForSearchString:searchString searchScope:searchOption];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
NSString* searchString = controller.searchBar.text;
return [self searchDisplayController:controller shouldReloadTableForSearchString:searchString searchScope:searchOption];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString*)searchString searchScope:(NSInteger)searchOption {
NSPredicate *predicate = nil;
if ([searchString length])
if (searchOption == 0) // full text, in my implementation. Other scope button titles are "Author", "Title"
predicate = [NSPredicate predicateWithFormat:@"title contains[cd] %@ OR author contains[cd] %@", searchString, searchString];
else
// docs say keys are case insensitive, but apparently not so.
predicate = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", [[controller.searchBar.scopeButtonTitles objectAtIndex:searchOption] lowercaseString], searchString];
[fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return YES;
}
PS.回答Vivas的问题,使用UISearchDisplayController会为重叠的过滤列表自动创建新的表视图。您可以按照文档所示检查使用的tableView,但在最简单的设置中,它只是起作用,因为fetchedResultsController在搜索的表视图中显示过滤版本或在您的表视图中显示所有数据。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
self.savedSearchTerm = searchText;
freshData = NO;
if (searchText !=nil)
{
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
else
{
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"All"];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[self.tableView reloadData];
// [searchBar resignFirstResponder];
// [_shadeView setAlpha:0.0f];
}