在核心数据中,我有一个到多个关系--每个布局都有许多行:

我试图通过将数据分组为布局来显示表视图中的数据:
Layout Nr 2342 (with orderPosition 1)
Line 1
Line 2
Line …
Layout Nr 2123 (with orderPosition 2)
Line 1
Line …
…我使用的NSFetchedResultsController如下所示:
NSEntityDescription *entity = [NSEntityDescription entityForName:@“Line” inManagedObjectContext:managedObjectContext];
NSSortDescriptor *orderPositionDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.orderPosition" ascending:YES];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setSortDescriptors:@[orderPositionDescriptor]];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='my_section'"]];
[fetchRequest setPredicate:predicate];
self.fetchController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"layout.groupNumber"
cacheName:nil];因此,我将fetch控制器设置为按布局groupNumber分组,还将排序描述符设置为按布局位置排序(每个布局都有自己的位置)。一旦数据被添加到核心数据中,它就会显示在表视图中。一切都很好,直到我尝试更改现有NSFetchedResultsController的NSFetchedResultsController谓词的时刻。
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='%@'",section]];
[self.fetchController.fetchRequest setPredicate:predicate]; 我在谓词中设置了与已经设置的完全相同的部分,在表中显示performFetch之后的数据仍然是相同的,但是布局(表节)改变了表中的位置,行显示在错误的布局中(基本上没有代码)。我发现,如果我将第一个排序描述符设置为sectionNameKeyPath - "layout.groupNumber",那么即使我更改了谓词,一切也会正常工作。但很明显,这不是orderPosition的命令,而是我想要达到的目标。有什么解决办法吗?任何帮助都将不胜感激。
发布于 2014-07-30 10:47:36
我找到了解决问题的办法。由于每个布局组号都有唯一的顺序位置,所以解决方案不是按groupNumber分组,而是按orderPosition分组。在这种情况下,我可以指定orderPosition作为第一个(也是唯一的)排序描述符。如果其他人有其他更好的解决方案,听起来还是很有趣的。
NSEntityDescription *entity = [NSEntityDescription entityForName:@“Line” inManagedObjectContext:managedObjectContext];
NSSortDescriptor *orderPositionDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.orderPosition" ascending:YES];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setSortDescriptors:@[orderPositionDescriptor]];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='my_section'"]];
[fetchRequest setPredicate:predicate];
self.fetchController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"layout.orderPosition"
cacheName:nil];发布于 2014-07-30 04:16:47
如果您在表视图中使用部分,并对表视图数据进行排序,那么重要的是要理解第一个排序描述符必须与sectionNameKeyPath相同。
因此,我建议您将代码更改为类似于此的代码。
NSEntityDescription *entity = [NSEntityDescription entityForName:@“Line” inManagedObjectContext:managedObjectContext];
//new line of code following...
NSSortDescriptor *sectionNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.groupNumber" ascending:YES];
NSSortDescriptor *orderPositionDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.orderPosition" ascending:YES];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//altered line of code following...
[fetchRequest setSortDescriptors:@[sectionNameDescriptor, orderPositionDescriptor]];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='my_section'"]];
[fetchRequest setPredicate:predicate];
self.fetchController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"layout.groupNumber"
cacheName:nil];我读过这篇文章,但不记得在哪里.所以没有文件证明我很抱歉。
这个有用吗?
https://stackoverflow.com/questions/24998075
复制相似问题