2013-08-21 14 views
9

エンティティが1つのセクションのテーブルビューに表示されています。エンティティには、workoutNametrainingLevelという2つの属性があります。どちらも文字列型です。トレーニングレベルは、1、2、3の3種類から構成されています(trainingLevel =(整数16またはString型?これはどちらが理想でしょうか?)3つのセクションに分割したいと思います。それぞれのセクションには、 ?。私はこれを行うのですか私は現在使用しているコードは以下の通りですどのようにfetchedResultsControllerの混乱を伴うiOS UITableViewセクション

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.workoutType.workouts.count; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    WorkoutSet *workoutSet = [self.fetchedResultsController objectAtIndexPath:indexPath]; 


    cell.textLabel.text = workoutSet.workoutName; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", workoutSet.days.count];  
} 

-(void)fetchWorkoutSets 
{ 

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkoutSet"]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"workoutType = %@", self.workoutType]; 

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"workoutName" ascending:YES]; 
    [fetchRequest setSortDescriptors:@[sortDescriptor]]; 
    [fetchRequest setPredicate:predicate]; 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] 
           initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext 
           sectionNameKeyPath:nil cacheName:nil]; 

    NSError *error; 
    if (![self.fetchedResultsController performFetch:&error]) 
    { 
     NSLog(@"Fetch failed: %@", error); 
    } 
} 

私は苦労しています何がある:

  • セクションごとに行数を決定する方法トレーニングレベル1または2または3のエントリの数をフェッチすることによって、コアデータモデルを介して実行されます。
  • 正しい項目を取得して各セクションの行を塗りつぶす方法。
  • 各セクションヘッダーにタイトルを付ける方法。ここで
+3

あなたはかなりあります。 'NSFetchedResultsController'のドキュメント、特に' trainingLevel'プロパティとして設定したい指定された初期化子の 'sectionNameKeyPath'パラメータを調べてください。 – Rog

+0

あなたの 'fetchedResultsController'ゲッターは' fetchWorkoutSet'ではなくフェッチを実行しなければならないことに注意してください。そうしないと、ワークアウトセットがフェッチされ、fetchedResultsが必要であるかどうかを確認する必要があります。 – memmons

答えて

17

fetchedResultsControllersを使用しての良いチュートリアルです:http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

あなたのコンテキストを保持するためにいくつかのプロパティを作成し、フェッチ:あなたのfetchedResultsController財産で

@property (nonatomic,strong)NSManagedObjectContext* managedObjectContext; 
@property (nonatomic,retain)NSFetchedResultsController *fetchedResultsController; 

、あなたのフェッチされた結果を設定するためにsectionKeyNamePathを使用セクション:

- (NSFetchedResultsController *)fetchedResultsController { 

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
              entityForName:@"Workouts" 
            inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
     initWithKey:@"workoutName" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
     [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
      managedObjectContext:managedObjectContext 
       sectionNameKeyPath:@"trainingLevel" 
         cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 

} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[self.fetchedResultsController sections] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = 
     [[[self fetchedResultsController] sections] objectAtIndex:section]; 

    return [sectionInfo numberOfObjects];   
} 

あなたは、あなたの管理を受けることができます。fetchedResultsControllerはあなたの-viewDidLoad:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSError *error; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Update to handle the error appropriately. 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 
} 

に起こることができますあなたは、その後のセクションと、このようなセクションの行数の数を返したいのあなたの初期集団このような特定の行のオブジェクト:

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

    // init the cell 
    // and whatever other setup needed 

    WorkoutSet *workoutSet = 
     [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    // configure the cell from the managedObject properties 
} 
+0

私は幸運で、上記のNSManagedObjectを使用すると言っている最後に向かって主に問題を抱えて上記を試して....どのように私はそれを介して自分のデータに到達することができます...ありがとう – user2512523

+1

@ user2512523それをもっと明確にする。あなたの 'WorkoutSet'オブジェクトは' fetchedResultsController'によって返されるものです。 – memmons

+0

@ user2512523この回答が役に立った場合は、受け入れ済みとマークして質問がまだ表示されないようにしてください。 – memmons

関連する問題