在 NSFetchedResultsController 中使用自定义部分? [英] Using custom sections with NSFetchedResultsController?

查看:35
本文介绍了在 NSFetchedResultsController 中使用自定义部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个带提取结果的分区表,但很难解决自定义部分的问题.

I'm making a sectioned table with fetched results, but am having a hard time getting custom sections worked out.

通常只有一个属性作为排序依据,并使用 sectionNameKeyPath: 来生成部分.但是我的排序属性是动态计算的,我似乎无法让 fetchedResultsController 正确使用它...

Normally one would just have an attribute to sort by, and use sectionNameKeyPath: to generate the sections. But my sorting attribute is calculated on the fly, and I can't seem to get the fetchedResultsController to use it correctly...

更新: 使用下面 jbrennan 的建议,我非常接近预期的功能.我向 NSDate 添加了一个类别,它返回一个几天前"的数字;把它放在这里会给我基于这些数字的部分:

Update: Using jbrennan's advice below, I'm really close to the intended functionality. I've added a Category to NSDate that returns a "days ago" number; putting that in here gives me sections based on those numbers:

NSFetchedResultsController *aFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
    managedObjectContext:managedObjectContext
    sectionNameKeyPath:@"myDateAttribute.daysAgo"
    cacheName:@"Root"];

这就是我卡住的地方:我不需要按几天前"对它们进行排序,我需要根据实体中的其他属性通过一些计算对它们进行排序.所以我不能只调用那个自定义 Category 方法,我需要调用一个带有 arguments 的方法,就像这样:

Here's where I'm stuck: I don't need them sorted by "days ago," I need them sorted via some calculations based on other attributes in the entity. So I can't just call that custom Category method, I need to call a method with arguments, like so:

[myDateAttribute sortingRoutine:thisObject.value]

或者类似的东西.我希望这在一定程度上有意义.如果你能提供帮助,非常感谢:)

Or something like that. I hope that makes some degree of sense. Thanks a ton if you can help :)

推荐答案

您可以尝试以下操作.

向任务实体中的核心数据模型添加瞬态属性.然后执行

Add a transient attribute to your core data model in the Task entity. Then implement the

- (void)awakeFromFetch

任务 NSManagedObject 类中的方法.请参阅其文档.在该方法中,您可以使用其他属性的值设置瞬态属性的值.请注意,您可以做什么有一些限制,但这在文档中有很好的解释(最值得注意的是,您不能修改关系或传递参数;但是,如果您可以仅使用其他属性的值来计算瞬态属性/关系应该没问题).

method in your Task NSManagedObject class. See its documentation. Within the method you are allowed to set a value for the transient property using the values of the other properties. Note that there are some restrictions on what you can do, but this is well explained in the documentation (most notably you can not modify relationships or pass arguments; however, if you can compute you transient property using only the values of the other properties/relationships it should be perfectly fine).

完成此操作后,您只需使用瞬态属性作为传递的属性即可取回部分.

Once you have done this, you simply use the transient property as the attribute you pass to get back the sections.

这篇关于在 NSFetchedResultsController 中使用自定义部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆