LoginSignup
6

More than 5 years have passed since last update.

NSFetchedResultsControllerの結果から関連を取得する

Last updated at Posted at 2012-05-28

UITableViewControllerを使っているときはNSFetchedResultsControllerを組み合わせるとなにかと便利
だけど、セルを描画するときに、あるEntityの関連の値をハメこみたかったらどうすれば?
と思ったら実は型変換するだけでできるよという話

※これをやるためには、あらかじめCoreDataで適切に関連付けを設定して、NSManagedObjectサブクラスをつくって、importしておく必要がある
その方法は以下に書いておいた
http://qiita.com/items/2460

fetchedResultsControllerは、表示したいEntityを取り込むそのままでOK

cellForRowAtIndexPathの部分で各行のデータをfetchedResultsControllerから取り出している部分で以下のように型変換をする

    UILabel *nameLabel   = (UILabel *)[cell viewWithTag:1];

    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    Memo *memo = (Memo *)managedObject;
    nameLabel = memo.name;
    // Memoの関連であるMemoTagを取り出す例
    for (MemoTag *tag in memo.tags) {
        NSLog(@"%@", tag);
    }

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6