在我的核心数据应用程序中,我使用 FetchedResultsController。通常要在 UITableView 中设置标题的标题,您将实现以下方法,如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
return [sectionInfo name];
}
其中 [sectionInfo name] 返回 NSString。
我的sectionKeyPath基于NSDate,除了它给我的节标题是原始日期描述字符串(例如12/12/2009 12:32:32 +0100)之外,这一切都工作得很好,看起来有点像标题困惑!
所以我想使用日期格式化程序来制作一个漂亮的标题,例如“Apr 17 2010”,但我无法使用 [sectionInfo name] 做到这一点,因为这是 NSString!有什么想法吗?
非常感谢
最佳答案
我找到了解决方案:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//Returns the title for each section header. Title is the Date.
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
NSArray *objects = [sectionInfo objects];
NSManagedObject *managedObject = [objects objectAtIndex:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *headerDate = (NSDate *)[managedObject valueForKey:@"itemDate"];
NSString *headerTitle = [formatter stringFromDate:headerDate];
[formatter release];
return headerTitle;
}
请看一下这个,如果你知道更好的方法请说!
否则,如果您遇到类似的问题,我希望这会有所帮助!
关于iphone - NSFetchedResultsController titleForHeaderInSection 与格式化的 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2660105/