0

我在 4 个实体之间使用排序描述符和集合谓词。

实体一:锻炼类型 关系锻炼 1 到许多 实体二:锻炼集 关系天数 1 到许多 实体三:锻炼日 关系锻炼 1 到许多 实体四:锻炼锻炼

倒数也设置了。

我收到的问题是 sortDescriptor 在幕后工作正常,但显示的内容不正确。

例如,在我的锻炼日实体中,我有字符串类型的属性 dayName。

存储值:第 1 天、第 2 天、第 3 天。

在 tableview 中运行显示值时:第 1 天、第 3 天、第 2 天。

当我从上到下单击天数时:

第 1 天 ....下一个视图控制器显示正确的第 1 天数据。

第 3 天 ....下一个视图控制器显示第 2 天的数据(不是第 3 天)。

第 2 天 ....下一个视图控制器显示第 3 天的数据(不是第 2 天)。

所以排序描述符正在工作,但显示的内容与选择的内容无关。

代码:

-(void)fetchWorkoutDays
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkoutDay"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"workoutSet = %@", self.workoutSet];
    [fetchRequest setPredicate:predicate];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dayName" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                 initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
                                 sectionNameKeyPath:nil cacheName:nil];
    //self.fetchedResultsController.delegate = self;
    NSError *error;
    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Fetch failed: %@", error);
    }

 }


- (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;
     }
     WorkoutDay *day = [self.workoutSet.days.allObjects objectAtIndex:indexPath.row];

     cell.textLabel.text = day.dayName;
     return cell;
}

感谢您的任何帮助。

如果需要更多代码,我可以显示它。

4

1 回答 1

0

workoutSet.days是一个无序集合,因此每次配置单元格时,您都会以随机顺序获取项目。您应该要求 FRC 为indexPath.

于 2013-08-20T18:38:26.433 回答