核心数据按格式日期排序表视图 [英] Core Data sort tableview by formatted date

查看:66
本文介绍了核心数据按格式日期排序表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何通过NsDate对tableview中的Core Data对象进行排序,但是默认情况下,这似乎为每个对象创建了一个新部分。我想使用NSDateFormatter按中等格式的日期对它们进行排序。我该怎么做?

I know how to sort Core Data objects in a tableview by NsDate, but this by default seems to create a new section for each object. I want to sort them by a medium formatted date with NSDateFormatter. How would I do this?

例如,如果我在同一天创建了3个对象,则希望它们位于同一节中,且节标题为一天,不需要时间。

For example, if I have 3 objects created on the same day, I want them to be in the same section with the section title being that Day, no time needed.

每个对象都有NSDate属性。谢谢您的帮助。

Each object has an NSDate property. Thanks for your help.

这是我在fetchedResultsController中包含rgeorge的建议的代码。我在这里错过了什么?

This is the code I have in fetchedResultsController with rgeorge's suggestions. What am I missing here?

- (NSFetchedResultsController *)fetchedResultsController {

if (fetchedResultsController != nil) {
    NSLog(@"get old fetched controller");

    return fetchedResultsController;
}

else{
    NSLog(@"get new fetched controller");
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"InTextEntity" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *dateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateModified" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:dateDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];


NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"mediumFormattedDate" cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![fetchedResultsController performFetch:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

}

return fetchedResultsController;

}

推荐答案

(我假设您使用的是 NSFetchedResultsController 来驱动表格视图,如果不这样做,我建议您进行检查。)

(I'll write this up assuming you're using an NSFetchedResultsController to drive your tableview. If you're not, I recommend checking it out.)

NSFetchedResultsController 的切片功能的一个有趣功能:尽管您可以对该属性进行排序上必须是一个模型化属性(因为sqlite会进行实际排序),所以您不必将各个部分分组。唯一的要求是分组必须与顺序一致。 (即,通过sort属性进行排序将使具有匹配组属性的对象彼此相邻。)

An interesting feature of NSFetchedResultsController's sectioning abilities: although the property you sort on must be a modeled property (because sqlite does the actual sorting), the property you group the sections with need not be. The only requirement is that the grouping be consistent with the ordering. (i.e., sorting by the sort property will put the objects with matching group properties next to each other.)

因此,只需在建模对象类中添加以下内容即可:

So just add something like this to your modeled object class:

// in interface
@property (nonatomic, readonly) NSString *mediumFormattedDate;

// in impl
-(NSString *)mediumFormattedDate
{
  // this can be fancier if you need a custom format or particular timezone: 
  return [NSDateFormatter localizedStringFromDate:self.date
                                        dateStyle:NSDateFormatterMediumStyle
                                        timeStyle:NSDateFormatterNoStyle];
}

(根本不需要在.xcdatamodel中提及mediumFormattedDate。)

(no need to mention mediumFormattedDate in the .xcdatamodel at all.)

然后继续按date属性对对象进行排序,但按新属性对它们进行分组。创建 NSFetchedResultsController 时,请遵循以下步骤:

Then go ahead and sort your objects by the date property, but group them by your new property. When you create your NSFetchedResultsController, do so along these lines:

NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"MyFancyEntity"];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"date"
                                                     ascending:YES];
[fr setSortDescriptors:[NSArray arrayWithObject:sd]];
NSFetchedResultsController *frc =
[[NSFetchedResultsController alloc] initWithFetchRequest:fr
                                    managedObjectContext:myManagedObjectContext
                                      sectionNameKeyPath:@"mediumFormattedDate"
                                               cacheName:nil];
// then do stuff with frc

做东西!我已经在一些应用程序中做到了这一点,以便进行日期分组,并且效果很好。

That's all it takes! I've done this in a few apps to get date grouping and it works well.

这篇关于核心数据按格式日期排序表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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