一个 NSFetchedResultsController ,日期为sectionNameKeyPath
我开发了一个使用 Core Data 的应用程序。在一个 UITableView 中,我想显示我的实体列表,按对象的保存日期排序。当我这样做时:
fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"date"
cacheName:nil];
我为每个对象获得一个新部分,因为此代码也根据秒对日期进行分组。但我想要一个按日期分组的对象列表,但仅根据日、月和年分组。这可能吗?如何实现?
非常感谢您的帮助!! ;)
I develop an application which uses Core Data. In one UITableView, I want to display a list of my entities, sorted by the saved date of the objects. When I do this:
fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"date"
cacheName:nil];
I get for each object a new section because this code groups the dates according to the seconds, too. But I want a list of the objects, grouped by date, but only according to the day, month and year. Is it possible and how?
Thank you very much for your help!! ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这应该对你有用:
[编辑]
刚刚看到你的评论,对于你想要实现的目标,你可以创建一个瞬态的
NSDate
属性(非持久)其格式与上述代码类似(即没有 H:mm:ss ZZZZ),并使用该属性作为您的sectionNameKeyPath
值。简而言之,对于具有
fooDate
和fooDateTransient
属性的foo
对象,您将:获取您的
foo.fooDate
属性使用上面的代码(或类似代码)对其进行转换,并将
NSDate
结果分配给foo.fooDateTransient
使用 <创建
fetchedResultsController
对象时,使用fooDateTransient
作为您的sectionNameKeyPath
。PS:我自己还没有测试过,但应该值得一试!
祝你好运,
罗格
This should do the trick for you:
[EDIT]
Jus saw your comment and for what you are trying to achieve, you could create a transient
NSDate
attribute (non persistent) that is formatted in a similar way to the above code (i.e. without H:mm:ss ZZZZ) and use that attribute as yoursectionNameKeyPath
value.So in a nutshell for a
foo
object, withfooDate
andfooDateTransient
attributes, you would:Get your
foo.fooDate
attributeTransform it using the code above (or similar) and assign the
NSDate
result tofoo.fooDateTransient
Use
fooDateTransient
as yoursectionNameKeyPath
when creating thefetchedResultsController
object.PS: I haven't tested this myself but should be worth a shot!
Good luck,
Rog
查看: http ://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009939
它适用于月份和年份,但很容易制作它适用于日、月和年。
Check out: http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009939
It works with month and year, but it's quite easy to make it work with day, month and year.
以下是按日期排序的 Swift 3 解决方案,但具有与各个日期相对应的部分标题。
daySectionIdentifier
的瞬态属性添加到核心数据中的实体。NSManagedObject
子类。删除可能在Entity+CoreDataProperties.swift
中生成的daySectionIdentifier
属性。在
Entity+CoreDataClass.swift
文件中,为daySectionIdentifier
添加以下 getter:在您的
UITableViewController
中> 实现,添加以下方法:NSFetchedResultsController
时,使用"daySectionIdentifier"
调用初始化程序> 作为sectionNameKeyPath
参数。NSFetchedResultsController
的排序描述符设置为实体的普通旧“date”
属性。重要的是,基于“date”
的排序顺序将与基于我们刚刚构造的节标识符的排序顺序一致。您现在应该将表视图按天分组为多个部分(例如“2017 年 2 月 6 日”),并按细粒度日期排序。
The following is a Swift 3 solution to sort by date but have section titles corresponding to individual days.
daySectionIdentifier
to your entity in Core Data.NSManagedObject
subclass. Delete the property fordaySectionIdentifier
that may get generated inEntity+CoreDataProperties.swift
.To the
Entity+CoreDataClass.swift
file, add the following getter fordaySectionIdentifier
:In your
UITableViewController
implementation, add the following method:NSFetchedResultsController
, call the initializer with"daySectionIdentifier"
as thesectionNameKeyPath
parameter.NSFetchedResultsController
's sort descriptor to your entity's plain old"date"
attribute. Importantly, the sort order based on"date"
will be consistent with the sort order based on the section identifier that we just constructed.You should now have your table view grouped into sections by day (e.g., "Feb 6, 2017"), and sorted by fine-grained date.
我认为这样会更好。
I think this would be better.
当遇到同样的问题时,我使用了@BoltClock's a Unicorn 和@Rog's anwser。
只需将一个瞬态 NSString *sectionTitle 添加到我的托管对象中,使用 @"sectionTitle" 作为sectionNameKeyPath 并创建一个自定义 getter,如下所示:
I used @BoltClock's a Unicorn and @Rog's anwser when having the same issue.
Simply added a transient NSString *sectionTitle to my managed object, used @"sectionTitle" as sectionNameKeyPath and created a custom getter like so: