使用带有搜索栏的获取结果控制器过滤表格视图?

时间:2014-03-22 02:10:59

标签: ios objective-c uitableview reloaddata

我有一个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;

}

我希望能够过滤结果,但出于几个不同的原因,我不想使用搜索栏和搜索显示控制器。

无论如何我可以使用我自己的谓词过滤结果,然后重新加载表吗?

2 个答案:

答案 0 :(得分:0)

获取结果后,您将通过UITableView填充新的数据数组,然后您可以致电:

[tableView reloadData];

答案 1 :(得分:0)

当然,您可以向NSFetchRequest添加NSPredicate,然后使用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];