エンティティが1つのセクションのテーブルビューに表示されています。エンティティには、workoutName
とtrainingLevel
という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のエントリの数をフェッチすることによって、コアデータモデルを介して実行されます。
- 正しい項目を取得して各セクションの行を塗りつぶす方法。
- 各セクションヘッダーにタイトルを付ける方法。ここで
あなたはかなりあります。 'NSFetchedResultsController'のドキュメント、特に' trainingLevel'プロパティとして設定したい指定された初期化子の 'sectionNameKeyPath'パラメータを調べてください。 – Rog
あなたの 'fetchedResultsController'ゲッターは' fetchWorkoutSet'ではなくフェッチを実行しなければならないことに注意してください。そうしないと、ワークアウトセットがフェッチされ、fetchedResultsが必要であるかどうかを確認する必要があります。 – memmons