构建一个 UITableView 的最佳方法是什么?该视图列出了按 NSDate 属性的月份定义的部分分组的 NSManagedObjects?
为了实现看似简单的事情,我已经奋斗了几个小时。
我有一个带有 NSDate 属性的实体 A。
我的目标是构建一个 UITableView,列出每个 A 对象,同时将它们分组为由其 NSDate 属性的 MM/YYYY 定义的部分。
我找到了这个苹果官方示例: http://developer.apple .com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html
所以:
- 我为我的实体 A 创建了一个新的瞬态可选属性“sectionTitleMonth”,
- 我定义了新的动态属性(primitiveDate 和primitiveSectionTitleMonth)并按照官方示例中的方式实现了它们的setter 和keyPathsForValuesAffectingSectionTitleMonth。
我决定将这个新代码移至类类别(A+nonGenerate),因为我不想要所有内容 每次我重新生成模型类时都会被清除。
之后,我更新了我的表视图委托,以便
- 它构建一个与示例中完全相同的 NSFetchedResultsController 。它现在使用我的瞬态属性作为sectionNameKeyPath
- ,它定义了与部分相关的方法,如官方示例中所示,
有两点让我想知道这个解决方案是否好:
1)我使用瞬态属性作为具有SQL Lite后端的sectionNameKeyPath。可以吗?我在某处读到瞬态属性 + SQLLite 不是好朋友,但不记得在哪种情况下
2)我在我的类类别 A+nonGenerate 中定义属性(primitiveDate 和 PrimitiveSectionTitleMonth)。可以吗?因为正如官方文档中所述“但是,您不能使用类别向类添加其他实例变量”。但这里的属性是动态的,我猜核心数据在生成 A 时会生成相关的实例变量,所以它应该可以正常工作......但我仍然有一点疑问:)
一切似乎都工作正常,但是......它是一个好的解决方案吗?我的意思是他们有更简单/更好的方法来实现我的目标吗?
请安慰我或者给我更好的解决方案:o)
I've been struggling for some hours to achieve what seemed like a simple thing.
I have an Entity A with a NSDate attribute.
My goal is to build a UITableView that lists every A object while grouping them into sections defined by the MM/YYYY of their NSDate attribute.
I found this Apple official sample: http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html
So:
- I created a new Transient Optional attribute "sectionTitleMonth" for my entity A
- I defined new dynamic properties (primitiveDate and primitiveSectionTitleMonth) and implemented their setters and keyPathsForValuesAffectingSectionTitleMonth as in the official sample.
I decided to move this new code to a Class Category (A+nonGenerated) because I didn't want everything
to be wiped out each time I regenerate my model classes.
After that I updated my table view delegate so that
- it builds a NSFetchedResultsController exactly as in the sample. It now uses my transient attribute as sectionNameKeyPath
- it defines the section related methods as in the official sample
2 points make me wonder if this solution is good:
1) I'm using a transient attributes as a sectionNameKeyPath with a SQL Lite back-end. Is it ok ? I've read somewhere that transient attributes + SQLLite weren't good friends but can't remember in which cases
2) I'm defining properties (primitiveDate and primitiveSectionTitleMonth) in my class category A+nonGenerated. Is it ok ? Because as stated in the official doc "You cannot, however, use a category to add additional instance variables to a class". But here the properties are dynamic and I guess that core data generates the related instance variables when it generates A so it should work fine... but I still have a little doubt :)
Everything seems to work fine but... is it a good solution ? I mean is their any simpler / better way to achieve my goal ?
Please comfort me or give me a better solution :o)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方法看起来不错。将访问器放在类别类文件中或在原始文件中没有明显的区别。您没有违反文档中的警告,因为您实际上并未将实例变量添加到模型类中。
Your approach seems fine. There is no apparent difference between having your accessors in the category class file or in the original. You are not violating the warning in the docs because you are not actually adding instance variables to the model class.