0

これをに追加しfetchedResultsControllerましたUIViewController。MagicalRecords を使用しています。

これは私のコードです:

- (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    _fetchedResultsController = [Artist fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"artist_id" ascending:NO delegate:self];

    return _fetchedResultsController;
}

しかし、このコードは呼び出しません。

私は私UITableViewの中に持っていUIViewControllerます。以下のこのメソッドは上記のメソッドを開始する必要があると思いますが、そうではありません。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id  sectionInfo =
    [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

したがって、目標は、魔法のレコードと を使用してデータをフェッチすることfetchedResultsControllerです。

もちろん、次のようなものを作成することもできますが、-findAllfetchedResultsController の代わりにデータが来ると自動的に更新されると思います-findAll

4

1 に答える 1

1

プロパティを宣言する必要があります (まだ行っていない場合)。

@property(strong, nonatomic) NSFetchedResultsController *fetchedResultsController;

ビューコントローラーで、インスタンス変数ではなくプロパティアクセサーを介してアクセスします。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

self.fetchedResultsControllergetter メソッドfetchedResultsControllerを呼び出して、FRC が最初の呼び出しで作成されるようにします。

また、設定する必要があります

_fetchedResultsController.delegate = self;

getter メソッド内で自動変更追跡を有効にし、呼び出します

[_fetchedResultsController performFetch:&error];

最初のフェッチ用 (MagicalRecord がそれを行う場合を除く)。

于 2013-11-07T08:26:15.090 に答える