UItableView按部分(和持久性)按日期排序 [英] UItableView sorted by date in sections (and persistence)

查看:131
本文介绍了UItableView按部分(和持久性)按日期排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个HistoryItems包含一个ID,标题和日期。
Date是一个NSDate,它的确切时间被添加到历史记录到第二个。



目前我只有一个NSArray的这些对象,最新的HistoryItem添加到NSArray的末尾。



如何使这些项目按照日期顺序显示在UItableView单元格中,最新的位于顶部,而且每天都必须拥有自己的部分。



我还需要这些HistoryItems在退出和加载应用程序之间持久存在,所以我应该把NSArray保存到文件或使用CoreData? >

解决方案

使用CoreData可能会更容易,因为您可以使用 NSFetchedResultsController 来管理部分'自动'。实际上使用 NSDate 作为一个sectionKeyPath可能有点问题,但运气有苹果有一些示例代码,您应该可以从每个月的每个部分到每个部分修改。



使用CoreData也可以免费提供基本的排序。


I have several HistoryItems which contain an ID, Title and Date. The Date is an NSDate of the exact time it was added to the history down to the second.

At the moment I just have an NSArray of these objects, the latest HistoryItem added to the end of the NSArray.

How do I make these items display in a UItableView cell in order of date, the latest being in the top, but also each day must have it's own section.

I also need these HistoryItems to be persistent between exit and loading of apps, so should I go with just saving the NSArray to a file, or using CoreData?

解决方案

Using CoreData will probably be easier since you can use NSFetchedResultsController to manage the sections 'automatically'. Actually using a NSDate as a sectionKeyPath can be a bit problematic but as luck has it Apple has some sample code that you should be able to modify from having a section per month to a section per day.

Going with CoreData also gives you the sorting essentially for free.

这篇关于UItableView按部分(和持久性)按日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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