1

私はコアデータのエンティティでは、この構造を有する:ソートnsfetchedresultscontroller

ID Region SubRegion SubRegionID CellText 
1  Face  Mouth   2    something 
2  Face  Eyes   1    ... 
3  Face  Nose   4    ... 
... 

をしかし、私は私のテーブルビュー(nsfetchedresultscontroller)はこのようにソートしたい:各セクション内

:IDプロパティでソートされた細胞を セクションtableView:SubRegionの名前でソートされたSubRegionの名前です。

どうすればこの問題を解決できますか?今では私はこれをやっているが、私は知らないsubregionIDでセクションタイトルをソートするためのベストプラクティスです。お時間を

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription 
           entityForName:@"DataBase" inManagedObjectContext:context]; 
[fetchRequest setEntity:entity]; 

NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(Region like '%@')", @"Face"]]; 
[fetchRequest setPredicate:requestPredicate]; 

NSSortDescriptor *ID = [[NSSortDescriptor alloc] 
          initWithKey:@"ID" ascending:YES]; 

[fetchRequest setSortDescriptors:[NSArray arrayWithObjects: ID, nil]]; 

[fetchRequest setFetchBatchSize:10]; 

fetchedResultsController = 
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
            managedObjectContext:context sectionNameKeyPath:@"SubRegion" 
               cacheName:nil]; 

fetchedResultsController.delegate = self; 

ありがとう!

答えて

0

述語には冗長性があります。むしろ

NSPredicate *requestPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(Region like '%@')", @"Face"]]; 

よりも、それだけで

NSPredicate *requestPredicate = 
    [NSPredicate predicateWithFormat:@"%K == %@", @"Region", @"Face"]; 

それはまた、あなたのNSFetchedResultsControllerを設定する方法で行う必要がありますする必要があります。 sectionNameKeyPathは「SubRegionID」ではなく「SubRegionID」として設定する必要があります。 tableView datasourceメソッドtitleForSection:では、IDに基づいてサブリージョンの名前を返す必要があります。

+0

ありがとうございます。しかし、問題はまだ私が使用したソートロジックに残っています。私はIDでソートされたセルを取得しますが、セクション名はSubRegionIDでソートされていません...私はsubregionIDでソートされた顔のサブリージョン(セクション)と、IDでソートされたセクション内のセルが必要です。アドバイスはありますか?ありがとう! – Samui

+0

これはあなたの 'NSFetchedResultsController'を設定する方法と関係しています。 '' SubRegion ''ではなく、 '' SubRegionID ''として' sectionNameKeyPath'を設定する必要があります。あなたの 'tableView datasource'メソッド' titleForSection: 'では、IDに基づいて部分領域の名前を返します。 – Mundi

+0

これは私が使った方法です。しかし、私はそれがsortdescriptorsを直接使用してソートできると思いました。アドバイスありがとうございます! – Samui

関連する問題