0

テーブルビューを並べ替えてセクション化するために、次の行があります。

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

上記は、セルをそれぞれのpositionSort値でソートする整数値です。セクション名を表示するための以下のコードもあります。ただし、セクションは、positionSortの順序ではなく、アルファベット順に表示されます。どうすればこれを修正できますか?

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"position" cacheName:@"Master"];

ありがとう!

更新:@MartinRのおかげで、私は答えにたどり着くことができました。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

    // Here I build up one of my Position objects using my unique position passed.
    // I had to cast the object as NSMutableString* to get rid of a warning
    Position * aPosition = [Position positionWithUniquePosition:(NSMutableString *)[[[sectionInfo objects] objectAtIndex: 0] position]
                                         inManagedObjectContext:self.managedObjectContext];

    // Once the above is done then I simply just accessed the attribute from my object
    return aPosition.positionDescription;

}
4

1 に答える 1

1

initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:ドキュメントから:

sectionNameKeyPath

...このキーパスがfetchRequestの最初のソート記述子で指定されたものと同じでない場合は、同じ相対順序を生成する必要があります。たとえば、fetchRequestの最初の並べ替え記述子は、永続プロパティのキーを指定する場合があります。sectionNameKeyPathは、永続プロパティから派生した一時プロパティのキーを指定する場合があります。

sectionNameKeyPathしたがって、数値の並べ替えと文字列の並べ替えでは同じ相対順序が生成されないため、並べ替え記述子で「positionSort」を使用したり、で「position」を使用したりすることはできません。

両方に「positionSort」を使用しtableView:titleForHeaderInSection:、位置番号ではなくセクションタイトルとして位置名を返すように変更します。

私はこれを自分で試しませんでしたが、このようなものはうまくいくはずです:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
    return [[[sectionInfo objects] objectAtIndex:0] position];
}
于 2012-08-13T09:06:37.957 に答える