我有一个NSFetchedResultsController
这是从 a 中获取对象NSManagedObjectContext
。我正在使用结果来填充 UITableView。
我正在用这两个排序描述符进行过滤。
NSSortDescriptor *lastOpened =
[[NSSortDescriptor alloc] initWithKey:@"lastOpened" ascending:NO];
NSSortDescriptor *titleDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
当我创建NSFetchedResultsController
,我通过以下方式对各部分进行排序sectionNameKeyPath:@"lastOpened"
.
现在我的部分显示标准格式,例如2009-07-02 20:51:27-0400由于没有两个可以同时打开,因此它们都是独一无二的。我需要它们涵盖日期/时间范围,例如一整天,并且采用人类可读的形式。就像是7 月 2 日,星期四.
Thanks!
Edit:
这一切都在一个UITableViewController
。这是更多代码。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the dates as section headings.
return [[[fetchedResultsController sections] objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
我最终添加了一个新属性day
to my NSManagedObject
子类来获取格式化的日期字符串。
@property (nonatomic, readonly) NSString * day;
@property (nonatomic, retain) NSDateFormatter * dateFormatter
合成日期格式化程序。 @synthesize dateFormatter;
我在 awakeFromFetch 和 awakeFromInsert 中初始化日期格式化程序。
self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
访问器用于day
.
- (NSString *)day {
NSDate *date = self.startDate;
return [self.dateFormatter stringFromDate:date];
}
然后我设置我的部分名称键路径来查看day
;您不需要对排序描述符进行任何更改。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)