iphone - NSFetchedResultsController titleForHeaderInSection 与格式化的 NSDate

标签 iphone nsdate nsfetchedresultscontroller nsdateformatter

在我的核心数据应用程序中,我使用 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/

相关文章:

ios - 检索不带前导零的当前日期

iphone - 可以从主包中删除文件吗?

iphone - iPhone上是否需要在代码中手动设置核心数据对象的逆关系

当下午时,Swift 时间返回为上午

cocoa - 将 NSDate 转换为 NSString 导致无法识别的选择器异常

ios - 从 NSDate 到 NSString 的转换,属性上的核心数据迁移未正确显示字符串

jquery - iPhone 故障,将 div 完美地移动到上面的 div

objective-c - 帮助解决编译器警告 : Initialization from distinct Objective-C type when types match

ios - 每次 NSFetchedResultsController 中对应于该单元格的对象发生更改时,我如何使我的单元格看起来像更新?

swift - 我可以根据子类对象的属性创建谓词吗?