1

Core Data ストアにメッセージのリストがあります (100 メッセージ)。

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchLimit:20];

必要な順序で最初の 20 件のメッセージのみが表示されます。私がする必要があるのは、作成日でソートされた最後の 20 件のメッセージを表示することですが、それらは TableView の下部から表示されるはずです。

Storage: 
1 : Test 1
2 : Test 2
3 : Test 3

It shows now as : 
1 : Test 1
2 : Test 2

**Supposed to show in table View:**

3 : Test 3
2 : Test 2
1 : Test 1

fetchedResultsController:

- (NSFetchedResultsController *)fetchedResultsController{

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Messages" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setFetchLimit:20];

    NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:YES];
    NSArray *sortDescriptors = @[sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(owner = %@) and (stream == %@) and (topic == %@)", ownerGuid,openStream,currentTopic];
    [fetchRequest setPredicate:predicate];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;

    self.fetchedResultsController = aFetchedResultsController;
    self.fetchedResultsController.delegate = self;

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

    return _fetchedResultsController;
}
4

2 に答える 2

0

@賢い答えは私のために働いています。私はNSIndexPath(newIndexPathを返す)だけを呼び出しました。のメソッドをオーバーライドしませんでしたobjectAtIndexPath。以下の方法で差し替えました。

- (NSIndexPath *)objectNewIndexPath:(NSIndexPath *)indexPath {

NSInteger sectionCount = [self.tableView numberOfRowsInSection:indexPath.section];
NSInteger newRowIndex = sectionCount - indexPath.row - 1;
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:indexPath.section];

return newIndexPath;
}

あとは交換するだけ

[self.fetchedResultsController objectAtIndexPath:[self objectNewIndexPath:indexPath];
于 2016-05-12T10:24:06.117 に答える
0

必要な作業は、使用する sortDescriptor を変更してascending:NO、最後の 20 メッセージを取得することだけです。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO];

これで、適切なメッセージが得られました。それらは配列内で逆になっています。次のコードを使用して、配列を簡単に逆にすることができます。

NSArray *messages = [[[fetchedMessages] reverseObjectEnumerator] allObjects];

編集:

このソリューションを使用するNSFetchedResultsControllerと、機能するために多くの追加コードが必要になります。

私が考えることができる最も簡単な解決策は、フェッチされた結果コントローラーからオブジェクトを取得するためのカスタム メソッドを実装することです。

- (id)objectAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger sectionCount = [self.tableView numberOfRowsInSection:indexPath.section];
    NSInteger newRowIndex = sectionCount - indexPath.row - 1;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:indexPath.section];

    return [self.fetchedResultsController objectAtIndexPath:newIndexPath];
}

あとは交換するだけ

[self.fetchedResultsController objectAtIndexPath:indexPath];

[self objectAtIndexPath:indexPath];
于 2013-07-31T22:17:09.043 に答える