iOS UITableView las secciones con la configuración de ffededResultsController
Tengo una entidad que se muestra en una vista de tabla en una sola sección. La entidad tiene dos atributos,workoutName
ytrainingLevel
. Ambos son de tipo cadena. El nivel de entrenamiento consta de los 3 tipos: 1, 2, 3. (trainingLevel = (Integer 16 o String Type? ¿Cuál sería el ideal?) Me gustaría dividir la tabla en tres secciones, cada sección contiene entradas para el nivel de entrenamiento correspondiente .
¿Cómo hago esto? El código que estoy usando actualmente es el siguiente:
- (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);
}
}
Con lo que estoy luchando es:
Cómo determinar el número de filas para cada sección a través del modelo de datos centrales obteniendo el número de entradas con nivel de entrenamiento 1 o 2 o 3.Cómo llenar las filas de cada sección obteniendo los elementos correctos.Cómo dar un título a cada encabezado de sección.