核心数据获取结果控制器和自定义节标题

发布于 2024-08-23 11:14:08 字数 345 浏览 9 评论 0原文

我熟悉管理表行的 NSFetchedResultsController 的常见和基本用法,但我有一个有趣的问题。

我的数据组织如下:时间表<--->>>日<--->>路线。我目前有一个表视图,其中包含时间表和管理时间表的创建和删除的获取控制器。我现在想为路线/天创建一个表视图。

我想在每一行列出一条路线,并创建与相关日期内的信息相对应的自定义部分标题。我可能可以通过获取所有路由来将某些内容组合在一起,然后按与相应日期的逆关系对它们进行分段,但我担心这样我将无法利用获取控制器委托方法来更新表管理对象发生变化。

有人有这方面的经验吗?关于如何前进有什么想法吗?

谢谢。

I am familiar with the common and basic use of a NSFetchedResultsController managing the rows of a table, but I have an interesting problem.

My data is organized as such: Schedule <--->> Day <--->> Route. I currently have a table view that has Schedules with a fetch controller managing the creation and deletion of Schedules. I now want to create a table view for Routes/Days.

I would like to list a Route on every row, and create custom section headers that correspond to information within the relevant Day. I could probably hack something together by fetching all the routes, and then sectioning them by the inverse relationship to there respective Day, but I am worried that then I will not be able to take advantage of the fetch controller delegate methods for updating the table when managed objects change.

Does anyone have any experience with this? Any ideas on how to move forward?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2024-08-30 11:14:08

NSFetchedResultsController 就是为此目的而设计的,因此您不必发明轮子。

当你初始化 NSFetchedResultsController 时,你应该考虑指定sectionNameKeyPath。该属性正是您自动生成该部分所需的属性。

对于你的情况,我会这样做:

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

你明白我的意思,如果你需要更多代码来澄清,请告诉我。

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.

不及他 2024-08-30 11:14:08

您需要通过为您创建的 NSSet 来研究“多对多”关系,该 NSSet 是为您创建的,用于表示数据库中的“Route->>Day”路径。假设你将你的关系命名为 isTraversedOn,并且将日期->时间表命名为 isScheduled,它会类似于:

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);
}

NSSet 仅保存指示对象的实际 id。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文