排序描述符无法正常或一致地运行 [英] Sort Descriptor Not functioning correctly or consistently
问题描述
我在 4 个实体之间使用排序描述符和集合谓词.
I am using a sort descriptor and a set predicate between 4 entities.
实体一:锻炼类型关系锻炼 1 对多实体二:锻炼集关系日 1 到多实体三:锻炼日关系练习 1 对多实体四:锻炼运动
entity one: workoutType Relationship workouts 1 to many entity two: workoutSet Relationship days 1 to many entity three: workoutDay relationship exercises 1 to many entity four: workoutExercise
也设置了逆.
我收到的问题是 sortDescriptor 在后台正常工作,但显示的内容不正确.
The problem i am receiving is that the sortDescriptor is working correctly behind the scenes, but what is displayed is incorrect.
例如,在我的 trainingDay 实体中,我有字符串类型的属性 dayName.
For example in my workoutDay entity i have attribute dayName of string type.
存储值:第 1 天、第 2 天、第 3 天.
stored values: day 1, day 2, day 3.
在 tableview 中运行时显示值:第 1 天、第 3 天、第 2 天.
when run shown values in tableview: day 1, day 3, day 2.
当我从上到下点击日期时:
When i click on the days from top to bottom:
第 1 天....下一个视图控制器显示正确的第 1 天数据.
day 1....next view controller shows correct day 1 data.
第 3 天....下一个视图控制器显示第 2 天的数据(不是第 3 天).
day 3....next view controller shows day 2 data (Not day 3).
第 2 天....下一个视图控制器显示第 3 天的数据(不是第 2 天).
day 2....next view controller shows day 3 data (Not day 2).
所以排序描述符正在工作,但显示的内容与选择的内容无关.
So the sort descriptor is working but what is shown does not correlate to what is being selected.
代码:
-(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;
}
感谢您的帮助.
如果需要更多代码,我可以显示.
If any more code is required i can show it.
推荐答案
workoutSet.days
是一个无序的集合,因此每次配置单元格时,您都会以随机顺序获取项目.您应该要求 FRC 为您返回 indexPath
的适当对象.
workoutSet.days
is a nun-ordered collection so each time you configure a cell you're getting the items in a random order. You should be asking the FRC to return you the appropriate object for the indexPath
.
这篇关于排序描述符无法正常或一致地运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!