У меня есть объект, отображаемый в виде таблицы только в одном разделе. Предприятие имеет два атрибута: workoutName
и trainingLevel
. Оба имеют строковый тип. Уровень подготовки состоит из 3 типов: 1, 2, 3. (trainingLevel = (Integer 16 или String Type? Что было бы идеальным?) Я хотел бы разбить таблицу на три раздела, каждый раздел содержит записи для соответствующего уровня обучения .?разделы iOS UITableView с fetchedResultsController confusion
Как это сделать код настоящее время я использую ниже:
- (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', в частности параметр' sectionNameKeyPath' указанного инициализатора, который вы хотите установить как свойство 'trainingLevel'. – Rog
Также обратите внимание, что ваш получатель 'fetchedResultsController' должен выполнять выборку, а не' fetchWorkoutSet', иначе у вас будет два разных вызова, если вам нужно убедиться, что набор тренировок был выбран, и вам нужны fetchedResults. – memmons