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

发布于 2024-10-20 20:45:10 字数 3053 浏览 1 评论 0原文

我已将搜索功能集成到我的应用程序中以查询 core data/.sqlite,并且工作正常。但我有一个问题,我不确定应该查看哪个类配置,有人可以指点我一下吗,谢谢

基本上我的模型是这个

TableView 1
显示产品类别
选择行--> TableView2

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

由于我在TableView 1中集成了UISearchBar,我希望当人们搜索他们想要的产品时,它会立即在表格中显示产品名称看法。我尝试过,但结果显示包含“搜索产品”的类别

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

    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
}

谢谢。

I have integrated the search function to my app to query core data/.sqlite and it works ok. But I have a problem and I am not sure which class configuration should I look at, could someone points me to the light, thanks

Basically my model is this

TableView 1
Display Product Category
selectRow --> TableView2

TableView 2
Display Products of selected Category
selectRow --> TableView3

As I integrated the UISearchBar in TableView 1, I wish to have the function when people search the product they want and it will show up the product's name right away in the table view. I tried, but the result is showing the Category which contains the "searched product".

So, how could I get this to show up correctly and which section of configuration should I look at?

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

Is it this part of the code?
Thanks

Update with more info:

Configure Cell

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

The Search Predicate

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

The Core Data Structure

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

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2024-10-27 20:45:10

如果您的数据模型具有 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 对象的子集。

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

self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];
    NSArray *distinctProducts=[self.filteredListContent valueForKey:@"[email protected]"];

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

If your data model has a Product entity and a Category entity and your fetches are returning Category objects instead of Product objects, then you have the wrong entity set for your fetch.

[ignore following as it applies to a different type of search -- techzen]
You usually create a separate fetch for the search because every time the users enters new characters in the search, the predicate for the fetch must change.

Update:

Okay, I misunderstood the type of search you were implementing. You are filtering the return of an existing fetch instead of fetching based on the entered search.

Looking at the predicate and data model in your update I think it clear that the predicate will only work against an array of Category objects. This:

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

... can only filter Category objects because only the Category object has an attribute of products. This predicate says, "Match all Catergory objects in which any related Product object has a nameEn attribute value that contains the search text."

Remember as well that the array you are filtering here:

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

...is an array of Category objects and not Product objects.

I think you need to rethink your UI design. Your TableView1 defaults to displaying a list of Category objects but you want your search of that table to display a list of Product objects. That will confuse the user. The user will expect a search on a table of Category objects to return a subset of Category objects.

However, with the existing design, you can produce an array of Product objects with the current code by creating a new array of Product objects by apply the @distinctUnionOfObjects collection operator:

self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];
    NSArray *distinctProducts=[self.filteredListContent valueForKey:@"[email protected]"];

... distinctProducts will now be an array of Product objects matching the search criteria. Use that array in configureCell:atIndexPath (you may need to resort it.)

万劫不复 2024-10-27 20:45:10

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

        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: ....etc 终止了 NSException.. 尽管 listofProductsName 的 NSLog 显示了正确的产品名称列表。

So, I tried these at the configure cell part

        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;  

But then I could convert the name to show...it said _NFSet isEqualToString: ....etc terminated NSException.. though the NSLog of listofProductsName show up the right products name list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文