Ios CoreData NSFetchedResultsController排序
我在排序我的Ios CoreData NSFetchedResultsController排序,ios,objective-c,core-data,nsfetchedresultscontroller,Ios,Objective C,Core Data,Nsfetchedresultscontroller,我在排序我的NSFetchedResultsController时遇到一个小问题 MyNSManagedObject有两个属性日期和开始时间 date在我的所有对象上都是时间00:00:00,这样,当使用date作为sectionNameKeyPath时,它会将日期相同(按天)的所有对象抓取到一个部分中。如果日期的时间不同,它会将每个对象放入不同的部分 这很有效,但在每个组中,我想按startTime对对象进行排序。因此,在每个部分中,它们分别从该日期的最早时间到最晚时间列出 我的问题是当使用d
NSFetchedResultsController
时遇到一个小问题
MyNSManagedObject
有两个属性<代码>日期和开始时间
date
在我的所有对象上都是时间00:00:00,这样,当使用date
作为sectionNameKeyPath
时,它会将日期相同(按天)的所有对象抓取到一个部分中。如果日期的时间不同,它会将每个对象放入不同的部分
这很有效,但在每个组中,我想按startTime
对对象进行排序。因此,在每个部分中,它们分别从该日期的最早时间到最晚时间列出
我的问题是当使用date
作为sectionNameKeyPath
和startTime
作为NSSortDescriptor时,它不喜欢它,而且玩起来很奇怪。例如,有时只是以不规则的方式显示某些数据
我认为这归结为排序描述符和sectionNameKeyPath
必须相同。我这样想对吗?如果没有,我应该如何设置我的NSFetchedResultsController
,以上述方式列出我的数据
谢谢
编辑:下面是代码。。。同样值得注意的是,当使用startTime
作为第二个排序描述符时,它会导致在我的tableview中使用nil对象显示重复项
NSFetchedResultsController
:
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor1, sortDescriptor2];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"date" cacheName:@"Master"];
cellforrowatinexpath
只是一个片段,显示我如何指定每个托管对象:
id <NSFetchedResultsSectionInfo> sectionInfo = [self.flightFetchedResultsController.sections objectAtIndex:indexPath.section];
NSArray *sectionFlights = [sectionInfo objects];
Flight *flight = [sectionFlights objectAtIndex:indexPath.row];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.flightFetchedResultsController.sections.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.flightFetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
id sectionInfo=[self.flightfetchedresultscoontroller.sections objectAtIndex:indexPath.section];
NSArray*sectionFlights=[sectionInfo对象];
Flight*Flight=[sectionFlights对象索引:indexath.row];
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回self.flightFetchedResultsController.sections.count;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
id sectionInfo=[[self.flightFetchedResultsController节]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
您的节键名路径需要与第一个排序描述符匹配
所以你可以
// sectionKeyNamePath = @"date".
NSSortDescriptor *dateSD = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
NSSortDescriptor *startTimeSD = [NSSortDescriptor sortDescriptorWithKey:@"startTime" ascending:YES];
frc.request.sortDescriptors = @[dateSD, startTimeSD];
如果这样做,它将按日期排序(和节),然后按开始时间对每个节进行排序
来自您的代码
您获取的对象不正确
要获取您需要使用的对象
Flight *flight = [self.frc objectAtIndexPath:indexPath];
获取的结果控制器知道其节和行。您不需要将其拆分。显示一些代码。
startTime
是字符串吗?(将其设置为日期)。它已经是日期,代码传入…您获取的对象不正确。我会更新我的答案。很有趣,但这会导致一些奇怪的结果。例如,在我的tableview中没有重复项。我已经在OP中添加了代码。我不这么认为,按照您的建议获取Flight
对象只会从第一部分(第一组对象)中获得经验。因此,对于表中的每个部分,您需要获得相应的部分对象。我觉得我可能已经解决了这个问题,一个完全干净的项目已经解决了。不过,感谢您提供匹配第一个排序描述符的提示,这是我不知道的!索引路径指向节和行。这就是它的工作原理。并不是我相信它的工作原理,而是它的工作原理。很高兴你把它整理好了。是的,如果你的数据集经常变化(如帖子上的评论等),那么你最好使用nil作为缓存名称:)@JoshKahane请注意,我使用的是objectAtIndexPath
而不是objectAtIndex
,这是有区别的。