Guy*_*Guy 17 core-data objective-c nsfetchedresultscontroller
过滤NSFetchedResultsController数据的最佳做法是什么?每次搜索栏的文本发生变化时,我是否需要重新初始化它?
我正在使用UISearchDisplayControllers和我正在实施:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;
Run Code Online (Sandbox Code Playgroud)
谢谢.
dk.*_*dk. 17
Guy的答案代码与问题有什么不同?据我所知,filterContentForSearchText:scope方法是由shouldReload方法调用的?
无论如何,这里有一些我在CoreDataBooks示例中添加的类似代码以包含搜索.在IB中为CoreDataBooks示例添加搜索显示控制器.然后我将代码添加到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;
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24204 次 |
| 最近记录: |