如何使用 NSFetchedResultsController 在日期之后生成部分

发布于 2024-12-11 05:37:32 字数 531 浏览 5 评论 0原文

我提供了一个托管对象列表,其中每个对象都有一个 timeStamp 属性。我想使用 timeStamp 属性按时间顺序对列表进行排序,这是我使用 NSSortDescriptor 所做的。但我也想基于整个日期生成部分(每天一个部分)

下面将给我一个基于二阶差异的部分,该部分太多了:

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

有没有一种方法可以使用 NSFetchedResultsController 从 timeStamp 属性生成部分格式如 yy-MM-dd?

感谢您的帮助

基督徒

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"];

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

Thanks for any help

Christian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2024-12-18 05:37:32

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

从 NSFetchedResultsController 设置 UITableView 标头

一个 NSFetchedResultsController,日期为sectionNameKeyPath

但是在 awakeFromFetch: 中创建一个类范围的 NSDateFormater,如下所示:

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

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

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

然后您获取的结果控制器是:

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

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

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.

Setting UITableView headers from NSFetchedResultsController

A NSFetchedResultsController with date as sectionNameKeyPath

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文