带有搜索功能的 NSFetchedResultsController [英] NSFetchedResultsController with search

查看:13
本文介绍了带有搜索功能的 NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过滤 NSFetchedResultsController 数据的最佳实践是什么?每次搜索栏的文本更改时都需要重新初始化吗?

What is the best practice to filter the NSFetchedResultsController data? do i need to re-initialize it every time the searchbar's text changes?

我正在使用 UISearchDisplayControllers 并且我正在实施:

I am using a UISearchDisplayControllers and i'm implementing:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;

谢谢.

推荐答案

Guy 的答案代码与问题有何不同?据我所知,filterContentForSearchText:scope 方法是由 shouldReload 方法调用的?

How is Guy's answer code any different from the question? As far as I can guess, the filterContentForSearchText:scope method is called by the shouldReload methods?

无论如何,这里有一些类似的代码,我添加到 CoreDataBooks 示例中以包含搜索.在 IB 中为 CoreDataBooks 示例添加一个搜索显示控制器.然后我在 RootViewController.m 中添加了如下代码:

Anyway, here's some similar code that I added in the CoreDataBooks sample to include search. Add a Search Display Controller in IB for the CoreDataBooks example. Then I added code to RootViewController.m as follows:

- (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;
}

附注.为了回答 Vivas,它使用 UISearchDisplayController 自动创建一个新的表视图来覆盖过滤列表.您可以检查正在使用哪个 tableView,如文档中所示,但在最简单的设置中它只是有效,因为 fetchedResultsController 在搜索的表视图中显示过滤版本或在表视图中显示所有数据.

PS. To answer Vivas, using a UISearchDisplayController it creates a new table view automatically for overlaying the filtered list. You can check which tableView is being used as shown in the docs, but in the simplest setup it just works because the fetchedResultsController is either showing a filtered version in the search's table view or showing all data in your table view.

这篇关于带有搜索功能的 NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆