iphone - 将 NSFetchedResultsController 与 UISearchBar 结合使用

标签 iphone objective-c cocoa-touch core-data nsfetchedresultscontroller

我的应用程序中有一个 UISearhBarController。它目前搜索在线数据库,但我正在将其更改为搜索核心数据数据库。它当前使用此代码:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{
    if (self.billingSearchBar == searchBar) {
        [self.billingSearchController filterResultsUsingString:searchText];
    }
}

- (void)filterResultsUsingString:(NSString *)filterString 
{
    self.billingSearchText = filterString;
    NSArray *billing_codes = [self billingSearch:self.billingSearchText searchType:self.billingSearchCategory];
    self.displayedBillingSearch = billing_codes;
    self.billingSearch = billing_codes; 
    [self.tableView reloadData];
}

-(NSMutableArray*)billingSearch:(NSString*)searchString searchType:(NSString*)searchType
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/server/20111115/60b88126/billing_search/", [self getHost]]];

    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
    [request setPostValue:searchString forKey:@"query"];
    [request setPostValue:searchType forKey:@"category"];

    [request startSynchronous];
    NSError *error = [request error];
    NSString *responseString;
    if (!error) {
        responseString = [request responseString];
    }

    NSMutableArray *search_results = [[NSMutableArray alloc] init];
    NSDictionary *responseDictionary = [responseString JSONValue];

    for (id key in [responseDictionary valueForKey:@"search_results"]) {
        [search_results addObject:key];
    }
    return search_results;  
}

所以我已经在核心数据中设置了数据库,我只需要将搜索/NSFetchedResults Controller 连接到它。有什么简单的方法吗?

最佳答案

最简洁的方法是使用两个 NSFetchedResultsController 并将“搜索”NSFRC 上的谓词设置为 UISearchBar 中的 searchText。决定使用哪个 NSFRC 后,只需设置 self.fetchedResultsController = "searching"NSFRC 或 "default"NSFRC。

您也可以使用相同的 NSFRC 并更改谓词,但这不是推荐的方法。

请参阅下面的评论。如果所有改变的只是谓词,一个 FRC 就可以了。

这是完成此操作的示例代码:

// First use the Searchbar delegate methods
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{   
    [self filterContentForSearchText:searchText];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self filterContentForSearchText:searchBar.text];
    [searchBar resignFirstResponder];
}

// The method to change the predicate of the FRC
- (void)filterContentForSearchText:(NSString*)searchText
{
    NSString *query = searchText;
    if (query && query.length) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@ or department contains[cd] %@", query, query];
        [self.fetchedResultsController.fetchRequest setPredicate:predicate];
        [self.fetchedResultsController.fetchRequest setFetchLimit:100]; // Optional, but with large datasets - this helps speed lots
    }

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Handle error
        exit(-1);
    }

    [self.myTableView reloadData];
}

这应该让你开始。

关于iphone - 将 NSFetchedResultsController 与 UISearchBar 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8539785/

相关文章:

iphone - 向 iTunesConnect 提交应用程序更新(疯狂!)

iPhone - NSPredicate(添加更多过滤器)

iphone - 为什么我的 UIVIew 会在屏幕外加载?

iphone - 更改 UITableview 中按下的特定按钮的图像

ios - 在iOS中捕获图像时如何显示纬度和经度

ios - 自定义字体可以在模拟器上使用,但不能在某些 Storyboard中渲染

ios - 如何获取 UITableView 中每个动态创建的单元格的文本字段值

iphone - 如何在 xcode 的文件模板中创建文件夹?

iphone - 为什么我的 UITableView 无法识别 UITableViewCell 中的 Touch?

iphone - iOS 跳转到不同的 View