带有格式化NSDate的NSFetchedResultsController titleForHeaderInSection [英] NSFetchedResultsController titleForHeaderInSection with formatted NSDate

查看:141
本文介绍了带有格式化NSDate的NSFetchedResultsController titleForHeaderInSection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Core Data应用程序中,我使用的是FetchedResultsController。通常要在UITableView中为标题设置标题,你可以实现以下方法:

   - (NSString *)tableView :( UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id< NSFetchedResultsSectionInfo> sectionInfo = [[< #Fetched results controller#> ]] objectAtIndex:section];
return [sectionInfo name];
}

其中[sectionInfo name]返回一个NSString。



我的sectionKeyPath基于一个NSDate,这一切都很好,除了它给我的部分标题是原始日期描述字符串(例如12/12/2009 12:32:32 +0100)在标题中看起来有点混乱!



所以我想在此使用日期格式化程序来制作一个很好的标题,如2010年4月17日,但我可以'用[sectionInfo name]做这个,因为这是NSString!任何想法?



非常感谢

解决方案

我找到了一个解决方案:

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//返回每个部分标题的标题。标题是日期。
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];
[格式化程序发布];
返回headerTitle;
}

请仔细看看,如果你知道更好的方法请说! / p>

否则如果您遇到类似问题,我希望这会有所帮助!


In my Core Data app I am using a FetchedResultsController. Usually to set titles for headers in a UITableView you would implement the following method like so:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
    return [sectionInfo name];
}

where [sectionInfo name] returns a NSString.

my sectionKeyPath is based on an NSDate and this all works fine apart from the section headers it gives me are the raw date description strings (e.g. 12/12/2009 12:32:32 +0100) which look a bit of a mess in a header!

So I want to use a date formatter on this to make a nice header like "Apr 17 2010" but I can't do that with the [sectionInfo name] as this is NSString! Any Ideas?

Many Thanks

解决方案

I found a solution:

- (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;
}

Please look over this, if you know of a better way please say!

Otherwise if your stuck with a similar problem I hope this helps!

这篇关于带有格式化NSDate的NSFetchedResultsController titleForHeaderInSection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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