1

エンティティメッセージがあります。各メッセージにはタイムスタンプが含まれています。最も古いメッセージから新しいメッセージまで、一度に20個の最後のメッセージのみを表示したいと思います。

最後の20を取得するには、次のコードを実行します

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
[fetchRequest setFetchLimit:20];
NSSortDescriptor *timeDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"time" ascending:NO] autorelease];
[fetchRequest setSortDescriptors:timeDescriptor];
NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"data" cacheName:nil] autorelease];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

しかし、結果として、昇順を表示したいと思います:YES。それを行う方法はありますか?

4

1 に答える 1

1

合計 # を取得して現在の行を減算して、逆の並べ替えを取得できませんでしたか。したがって、0 -> 20、1 -> 19、... 20 -> 0. 1 つのセクションで tableView を使用していると仮定すると、コードは次のようになります (未テスト):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
NSInteger reverseRow = [sectionInfo numberOfObjects] - indexPath.row - 1;
NSIndexPath *reverseIndexPath = [NSIndexPath indexPathForRow:reverseRow inSection:indexPath.section];    
id myObject = [self.fetchedResultsController objectAtIndexPath:reverseIndexPath];
}
于 2013-01-28T23:51:27.283 に答える