核心数据,搜索控制器和表视图

时间:2011-03-07 19:13:57

标签: iphone core-data uisearchbar uisearchdisplaycontroller

我已将搜索功能集成到我的应用程序中以查询核心数据/ .sqlite,它运行正常。但我有一个问题,我不确定应该看哪个类配置,有人可以指点我,谢谢

基本上我的模型就是这个

TableView 1
显示产品类别
selectRow - > TableView2

TableView 2
显示所选类别的产品
selectRow - > TableView3

当我将UISearchBar集成到 TableView 1 中时,我希望在人们搜索他们想要的产品时拥有该功能,它将立即显示产品名称表视图。我试过,但结果是显示类别,其中包含“搜索过的产品”。

那么,我怎样才能正确显示它以及我应该看哪个配置部分?

    UISearchDisplayController *searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];  

[self performSelector:@selector(setSearchDisplayController:) withObject:searchDisplayController];

[searchDisplayController setDelegate:self];  
[searchDisplayController setSearchResultsDataSource:self];  
[searchDisplayController setSearchResultsDelegate:self];
[searchDisplayController release];  
[self.tableView setContentOffset:CGPointMake(0,self.searchDisplayController.searchBar.frame.size.height)];

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();

    self.filteredListContent = [NSMutableArray arrayWithCapacity:[[[self fetchedResultsController] fetchedObjects] count]];
}   

这是代码的这一部分吗?
感谢

更新信息:

配置单元格

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *entity = nil;
if (self.searchIsActive){  // Configure the cell to show the searched item's name  
entity = [[self filteredListContent] objectAtIndex:[indexPath row]];
cell.textLabel.textColor = [UIColor blackColor];
} else {// Configure the cell to show the category's name 
cell.textLabel.textColor = [UIColor blackColor];  
entity = [fetchedResultsController objectAtIndexPath:indexPath]; 
} 
cell.textLabel.text = [entity valueForKey:@"nameEN"]; 
}  

搜索谓词

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"ANY products.nameEN CONTAINS[cd] %@", searchText];
self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];
}

核心数据结构

Category{  
    nameEN  
    products <- one to many relation ->> Product.productcat
}

Product{ 
    nameEN  
    spec  
    productcat <<-- many to one relation-> Category.products
}

谢谢。

2 个答案:

答案 0 :(得分:1)

如果您的数据模型具有Product实体和Category实体,并且您的提取正在返回Category个对象而不是Product个对象,那么您的实体设置错误为你的提取。

[忽略以下内容,因为它适用于其他类型的搜索 - techzen] 您通常会为搜索创建单独的提取,因为每次用户在搜索中输入新字符时,提取的谓词都必须更改。

更新

好的,我误解了你正在实施的搜索类型。您正在过滤现有提取的返回,而不是根据输入的搜索进行提取。

查看更新中的谓词和数据模型,我认为谓词只会对Category个对象的数组起作用。这样:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"ANY products.nameEN CONTAINS[cd] %@", searchText];

...只能过滤Category个对象,因为只有Category对象的属性为products。该谓词说:“匹配所有Catergory对象,其中任何相关的Product对象都具有包含搜索文本的nameEn属性值。”

请记住您在此处过滤的数组:

self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];

...是Category个对象的数组,而不是Product个对象。

我认为您需要重新考虑您的UI设计。您的TableView1默认显示Category个对象的列表,但您希望搜索该表以显示Product个对象的列表。这会让用户感到困惑。用户希望在Category个对象的表上搜索以返回Category个对象的子集。

但是,使用现有设计,您可以通过应用Product集合运算符创建一个新的Product对象数组,使用当前代码生成@distinctUnionOfObjects个对象数组: / p>

self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];
    NSArray *distinctProducts=[self.filteredListContent valueForKey:@"products.@distinctUnionOfObjects.enName"];

... distinctProducts现在将是符合搜索条件的Product个对象数组。在configureCell:atIndexPath中使用该数组(您可能需要使用它。)

答案 1 :(得分:0)

所以,我在配置单元部分尝试了这些

        NSDictionary *distinctProducts=[self.filteredListContent valueForKey:@"products"];
    NSLog(@"what are products:%@",distinctProducts);
    NSArray *listofProductsName = [distinctProducts valueForKey:@"nameEN"];
    NSLog(@"whatup: %@",listofProductsName);
    NSArray *entity = [listofProductsName objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.text = entity;  

然后我可以将名称转换为显示...它说_NFSet isEqualToString:....等终止NSException ..虽然listofProductsName的NSLog显示正确的产品名称列表。