使用没有搜索栏的获取结果控制器过滤表视图?
Posted
技术标签:
【中文标题】使用没有搜索栏的获取结果控制器过滤表视图?【英文标题】:Filtering table view using fetched results controller without Search Bar? 【发布时间】:2014-03-22 02:10:58 【问题描述】:我有一个 UITableView 使用 NSFetchedResults 控制器来填充表格,如下所示:
- (NSFetchedResultsController *)fetchedResultsController
if (_fetchedResultsController != nil)
return _fetchedResultsController;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"expenseDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
self.fetchedResultsController.delegate = self;
return _fetchedResultsController;
我希望能够过滤结果,但出于几个不同的原因,我不想使用搜索栏和搜索显示控制器。
无论如何我可以使用自己的谓词过滤结果然后重新加载表吗?
【问题讨论】:
【参考方案1】:获取结果后,您将通过UITableView
填充一个新的数据数组,然后您可以调用:
[tableView reloadData];
【讨论】:
当然——但是我该如何获取这些新结果呢?我可以在 fetch 控制器上做一个新的 fetch 请求并执行它吗?【参考方案2】:当然,您可以将 NSPredicate 添加到您的 NSFetchRequest 中,然后使用您的 NSFetchedResultsController 执行新的提取。
这是我做过类似事情的一个例子:
- (NSFetchedResultsController *)fetchedResultsController
if (_fetchedResultsController != nil)
return _fetchedResultsController;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"expenseDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
//Apply your filtering predicate to the fetchRequest if necessary
NSPredicate *filterPrediate = [self getFilterPredicate];
if(filterPrediate)
[fetchRequest setPredicate:filterPrediate];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
- (NSPredicate *)getFilterPredicate
//Get or create your filtering predicate here.
//You can combine NSPredicates using a NSCompoundPredicate if necessary.
if(shouldFilter)
return yourNSPrecicate;
else
return nil;
- (void)reloadFetchedViewController
NSError *error;
if (![self.fetchedResultsController performFetch:&error])
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
然后用新的查询更新 UITableView:
self.fetchedResultsController = nil;
[self reloadFetchedViewController];
[self.tableView reloadData];
【讨论】:
另一个附加说明:永远不要在自己的实例化方法中使用 self.fetchedResultsController。改用 _fetchedResultsController以上是关于使用没有搜索栏的获取结果控制器过滤表视图?的主要内容,如果未能解决你的问题,请参考以下文章