从NSFetchedResultsController设置UITableView标头

Mic*_*ich 7 iphone cocoa cocoa-touch core-data

我有一个NSFetchedResultsController从a获取对象NSManagedObjectContext.我正在使用结果来填充UITableView.

我正在使用这两个排序描述符进行过滤.

NSSortDescriptor *lastOpened = 
    [[NSSortDescriptor alloc] initWithKey:@"lastOpened" ascending:NO];

NSSortDescriptor *titleDescriptor = 
    [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
Run Code Online (Sandbox Code Playgroud)

当我创建时NSFetchedResultsController,我会对这些部分进行排序sectionNameKeyPath:@"lastOpened".

现在我的部分显示标准格式,如2009-07-02 20:51:27 -0400,因为没有两个可以同时打开,它们都是独一无二的.我需要它们来涵盖日期/时间范围,例如一整天,并且是人类可读的形式.像7月2日星期四那样的东西.

谢谢!


编辑:

这都是在里面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];
}
Run Code Online (Sandbox Code Playgroud)

小智 12

我最后day在我的NSManagedObject子类中添加了一个新属性来获取格式化的日期字符串.

@property (nonatomic, readonly) NSString * day;
@property (nonatomic, retain) NSDateFormatter * dateFormatter
Run Code Online (Sandbox Code Playgroud)

Sythesize dateFomatter.@synthesize dateFormatter;

我在awakeFromFetch和awakeFromInsert中初始化日期格式化程序.

self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
Run Code Online (Sandbox Code Playgroud)

的访问者day.

- (NSString *)day {
  NSDate *date = self.startDate;
  return [self.dateFormatter stringFromDate:date];
}
Run Code Online (Sandbox Code Playgroud)

然后我设置我的部分名称keypath来查看day; 您不应该对排序描述符进行任何更改.