5

CoreDataに関連して、どこから来たのかわからない問題が発生しています。私のデータベースには、(1対多の関係を使用して)要素を含む一連のカテゴリ(名前と説明付き)があります。

クラスの属性を指定してテーブルビューをセクションに分割したいのですが、Categoryを使用して実行しようとするとsectionNameKeyPath:、結果のNSFetchedResultsControllerセクションは0になります。このパラメーターにnilを渡すと、1つのセクションがあります。

コードは次のとおりです。

- (NSFetchedResultsController*) fetchedResultsController
{
    if(fetchedResultsController)
        return fetchedResultsController;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:10];

    // Edit the sort key as appropriate.

    NSSortDescriptor *checkDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checked"
                                                                   ascending:YES];
    NSSortDescriptor *indexDescriptor = [[NSSortDescriptor alloc] initWithKey:@"orderIndex"
                                                                   ascending:YES];
    NSArray *sortDescriptors = @[checkDescriptor, indexDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                   managedObjectContext:self.managedObjectContext
                                                                     sectionNameKeyPath:@"checked"
                                                                              cacheName:nil];

    NSError *error = nil;
    if (![fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
        return nil;
    } else {
        fetchedResultsController.delegate = self;
        return fetchedResultsController;
    }
}
4

1 に答える 1

1

NSFetchedResultsControllerドキュメントを参照してください。使用されるキーsectionNameKeyPath(この場合は「name」)は、最初のソート記述子で使用されるキー(この場合は「checked」)と同じである必要があります。それらは異なる場合がありますが、両方のキーが同じ相対順序を生成する必要があります。

あなたの場合、「name」に追加のソート記述子を追加し、それを最初のソート記述子として使用するとします。

于 2012-08-23T11:55:53.910 に答える