我在 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;
}
感谢您的任何帮助。
如果需要更多代码,我可以显示它。