从NSFetchedResultsController设置UITableView头 [英] Setting UITableView headers from NSFetchedResultsController

查看:90
本文介绍了从NSFetchedResultsController设置UITableView头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSFetchedResultsController ,它从 NSManagedObjectContext 中获取对象。我使用结果填充UITableView。

I have a NSFetchedResultsController which is fetching objects from a NSManagedObjectContext. I'm using the results to populate a UITableView.

我用这两个排序描述符过滤。

I'm filtering with these two sort descriptors.

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日星期四

Right now my sections display the standard format like 2009-07-02 20:51:27 -0400 and since no two can be opened at the same time, they are all unique. I need them to cover range of date/times, such as an entire day, and be in a human-readable form. Something like Thursday, July 2.

感谢!

修改

这一切都在 UITableViewController 这里是一些更多的代码。

This is all inside a UITableViewController. Here's some more code.

- (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 到我的 NSManagedObject 子类以获取格式化的日期字符串。

I ended up adding a new property day to my NSManagedObject subclass to get a formatted date string.

@property (nonatomic, readonly) NSString * day;
@property (nonatomic, retain) NSDateFormatter * dateFormatter

合并dateFomatter。
@synthesize dateFormatter;

Sythesize the dateFomatter. @synthesize dateFormatter;

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

I initialize the date formatter in awakeFromFetch and awakeFromInsert.

self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];

day 的存取器。

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

然后我设置我的section name keypath来查看;您不需要对排序描述符进行任何更改。

I then set my section name keypath to look at day; you shouldn't need to make any changes to your sort descriptors.

这篇关于从NSFetchedResultsController设置UITableView头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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