1

コア データ/.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
}

ありがとうございました。

4

2 に答える 2

1

データ モデルにProductエンティティとCategoryエンティティがあり、フェッチCategoryでオブジェクトではなくオブジェクトが返されるProduct場合は、フェッチに間違ったエンティティが設定されています。

[異なるタイプの検索に適用されるため、以下は無視-- techzen] ユーザーが検索で新しい文字を入力するたびに、フェッチの述語を変更する必要があるため、通常は検索用に個別のフェッチを作成します。

アップデート:

わかりました。実装している検索の種類を誤解していました。入力した検索に基づいてフェッチするのではなく、既存のフェッチの戻りをフィルタリングしています。

更新の述語とデータ モデルを見ると、述語がオブジェクトの配列に対してのみ機能することは明らかだと思いCategoryます。これ:

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

... オブジェクトのみが の属性を持っているCategoryため、オブジェクトのみをフィルタリングできます。この述語は、「検索テキストを含む nameEn 属性値が関連する Product オブジェクトに含まれるすべての Catergory オブジェクトに一致する」ことを示しています。Categoryproducts

ここでフィルタリングしている配列についても覚えておいてください。

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

Category...オブジェクトではなく、オブジェクトの配列Productです。

UIデザインを再考する必要があると思います。あなたの TableView1 はデフォルトでCategoryオブジェクトのリストを表示しますが、そのテーブルの検索でオブジェクトのリストを表示したいとしProductます。それはユーザーを混乱させます。Categoryユーザーは、オブジェクトのテーブルを検索すると、オブジェクトのサブセットが返されることを期待しCategoryます。

ただし、既存の設計では、コレクション演算子を適用してオブジェクトProductの新しい配列を作成することにより、現在のコードでオブジェクトの配列を生成できます。Product@distinctUnionOfObjects

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

...検索条件に一致するオブジェクトdistinctProductsの配列になります。Productその配列を使用しconfigureCell:atIndexPathます (再分類する必要がある場合があります)。

于 2011-03-08T23:10:04.817 に答える
0

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.

于 2011-03-09T18:30:21.910 に答える