核心数据、搜索控制器和表视图
我已将搜索功能集成到我的应用程序中以查询 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的数据模型具有
Product
实体和Category
实体,并且您的提取返回的是Category
对象而不是Product
对象,那么您的提取实体集是错误的。[忽略以下内容,因为它适用于不同类型的搜索 -- techzen]
您通常会为搜索创建单独的提取,因为每次用户在搜索中输入新字符时,提取的谓词都必须更改。更新:
好的,我误解了您正在实现的搜索类型。您正在过滤现有提取的返回结果,而不是根据输入的搜索进行提取。
查看更新中的谓词和数据模型,我认为很明显谓词仅适用于
Category
对象数组。这:... 只能过滤
Category
对象,因为只有Category
对象具有products
属性。该谓词表示:“匹配所有 Catergory 对象,其中任何相关 Product 对象都具有包含搜索文本的 nameEn 属性值。”还要记住,您在此处过滤的数组:
...是
Category
对象的数组,而不是Product
对象的数组。我认为你需要重新考虑你的 UI 设计。您的 TableView1 默认显示
Category
对象列表,但您希望对该表的搜索显示Product
对象列表。这会让用户感到困惑。用户期望对Category
对象表进行搜索以返回Category
对象的子集。但是,根据现有设计,您可以通过应用
@distinctUnionOfObjects< 创建一个新的
Product
对象数组,从而使用当前代码生成一个Product
对象数组。 /code> 集合运算符:...
distinctProducts
现在将是与搜索条件匹配的Product
对象的数组。在configureCell:atIndexPath
中使用该数组(您可能需要使用它。)If your data model has a
Product
entity and aCategory
entity and your fetches are returningCategory
objects instead ofProduct
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:... can only filter
Category
objects because only theCategory
object has an attribute ofproducts
. 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:
...is an array of
Category
objects and notProduct
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 ofProduct
objects. That will confuse the user. The user will expect a search on a table ofCategory
objects to return a subset ofCategory
objects.However, with the existing design, you can produce an array of
Product
objects with the current code by creating a new array ofProduct
objects by apply the@distinctUnionOfObjects
collection operator:...
distinctProducts
will now be an array ofProduct
objects matching the search criteria. Use that array inconfigureCell:atIndexPath
(you may need to resort it.)所以,我在配置单元部分尝试了这些
,但是然后我可以将名称转换为显示...它说 _NFSet isEqualToString: ....etc 终止了 NSException.. 尽管 listofProductsName 的 NSLog 显示了正确的产品名称列表。
So, I tried these at the configure cell part
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.