如何使用 NSFetchedResultsController 在日期之后生成部分 [英] How to use NSFetchedResultsController to generate sections after dates

查看:49
本文介绍了如何使用 NSFetchedResultsController 在日期之后生成部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在展示一个托管对象列表,其中每个对象都有一个 timeStamp 属性.我想使用我使用 NSSortDescriptor 执行的 timeStamp 属性按时间顺序对列表进行排序.但我也想根据整个日期生成部分(每天一个部分)

I am presenting a list of managed objects where each has a timeStamp property. I want to sort the list chronologically using the timeStamp property which i do with a NSSortDescriptor. But I also want to generate sections based on whole dates (one section for every day)

以下将根据第二个差异为我提供一个部分,该部分太多了:

The following will give me one section based on second-differences which is too many sections:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"timeStamp" cacheName:@"PatientDetailViewCache"];

有没有办法从 timeStamp 属性和 NSFetchedResultsController 生成格式为 yy-MM-dd 的部分?

Is there a way to generate sections from the timeStamp property with the NSFetchedResultsController that are formatted like yy-MM-dd?

感谢您的帮助

基督徒

推荐答案

最简单的方法是在您的子类 NSManagedObject 上为格式化的日期创建一个属性并使用该属性进行排序.有很多关于 SO 的问题都有类似的问题.

The easiest way is on your subclassed NSManagedObject create a property for a formatted date and sort using that property. There are many questions on SO with similar questions.

从 NSFetchedResultsController 设置 UITableView 标头

一个 NSFetchedResultsController,日期为 sectionNameKeyPath

但是在 awakeFromFetch: 中创建一个类宽的 NSDateFormater 像这样:

But create a class wide NSDateFormater in the awakeFromFetch: like so:

-(void)awakeFromFetch{
    dateFormater = [[NSDateFormatter alloc] init];
    [dateFormater setDateFormat:@"yy-MM-dd"];
    [super awakeFromFetch];
}

然后在该类属性的访问器中执行如下操作:

then in the accessor for that class property do something like this:

-(NSString*)myprop{
    if(myprop==nil){
        myprop = [dateFormat stringFromDate:self.OTHERDATE];
    }
    return myprop;
}

那么你获取的结果控制器是:

Then your fetched results controller is:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"myprop" cacheName:@"PatientDetailViewCache"];

然后它将返回格式化的日期并基于此进行排序.

Then it will return the formatted date and sort based on that.

这篇关于如何使用 NSFetchedResultsController 在日期之后生成部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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