Core Data Fetched Results Controller and Custom Section Header

非 Y 不嫁゛ 提交于 2019-12-03 16:55:21

The NSFetchedResultsController is designed for this purpose, so you don't have to invent the wheel.

When you initialize your NSFetchedResultsController, you should consider specifying the sectionNameKeyPath. That property is exactly what you need to automatically generating the section for you.

In your case, I would do as follows:

NSEntityDescription: @"Route"
NSPredicate: @"day = %@"
NSFetchedResultsController: sectionNameKeyPath:@"route.day"

You see what I mean, if you need more code for clarification, tell me.

You need to research following the To Many relationship through the NSSet that is created for you to represent the Route->>Day path in your database. Assuming you named your relationship isTraversedOn, and the day->Schedule as isScheduled, it would be something like:

Route *route=[self.fetchedResultsController.fetchedObjects objectAtIndex:0];
for (Day *day in route.isTraversedOn) {
   Schedule *schedule = day.isScheduled;
   NSLog(@"route is traversed on %@ according to schedule %@",day.name,schedule.name);
}

the NSSet just saves the actual id's of the indicated objects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!