日期为sectionNameKeyPath的NSFetchedResultsController [英] A NSFetchedResultsController with date as sectionNameKeyPath

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

问题描述

我开发了一个使用Core Data的应用程序。在一个UITableView中,我想显示我的实体列表,按对象的保存日期排序。当我这样做:

  fetchedResultsController = [[NSFetchedResultsController alloc] 
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@date
cacheName:nil];

我为每个对象获取一个新的部分,因为这个代码也根据秒来分组日期。但我想要一个对象的列表,按日期分组,但只能根据日,月和年。是可能和如何?



非常感谢您的帮助! ;)

解决方案

这应该为你打电话:

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString * rawDateStr = [[self.fetchedResultsController sections] objectAtIndex:section] ;
//将rawDateStr字符串转换为NSDate ...
NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@yyyy-MM-dd HH:mm:ss ZZ];
NSDate * date = [formatter dateFromString:rawDateStr];

//将NSDate转换为我们想要的格式...
[formatter setDateFormat:@d MMMM yyyy];
NSString * formattedDateStr = [formatter stringFromDate:date];
return formattedDateStr;
}


$ b

Jus看到了你的意见和你想要实现的,你可以创建一个暂时的 NSDate 属性(非持久性)与上述代码类似的方式(即没有H:mm:ss ZZZZ),并将该属性用作 sectionNameKeyPath 值。



因此,对于 foo 对象,使用 fooDate fooDateTransient 属性,您将:


  1. 获取您的 foo.fooDate 属性


  2. 使用上述代码(或类似代码)转换它,并分配 NSDate 结果到 foo.fooDateTransient


  3. 使用 fooDateTransient


  4. sectionNameKeyPath ol>

    PS:我没有自己测试,但值得一试!



    祝你好运,
    Rog


    I develop an application which uses Core Data. In one UITableView, I want to display a list of my entities, sorted by the saved date of the objects. When I do this:

    fetchedResultsController = [[NSFetchedResultsController alloc]
                                initWithFetchRequest:fetchRequest
                                managedObjectContext:managedObjectContext
                                  sectionNameKeyPath:@"date"
                                           cacheName:nil];
    

    I get for each object a new section because this code groups the dates according to the seconds, too. But I want a list of the objects, grouped by date, but only according to the day, month and year. Is it possible and how?

    Thank you very much for your help!! ;)

    解决方案

    This should do the trick for you:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      NSString *rawDateStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
      // Convert rawDateStr string to NSDate...
      NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
      [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZ"];
      NSDate *date = [formatter dateFromString:rawDateStr];
    
      // Convert NSDate to format we want...
      [formatter setDateFormat:@"d MMMM yyyy"];
      NSString *formattedDateStr = [formatter stringFromDate:date];
      return formattedDateStr;  
    }
    

    [EDIT]

    Jus saw your comment and for what you are trying to achieve, you could create a transient NSDate attribute (non persistent) that is formatted in a similar way to the above code (i.e. without H:mm:ss ZZZZ) and use that attribute as your sectionNameKeyPath value.

    So in a nutshell for a foo object, with fooDate and fooDateTransient attributes, you would:

    1. Get your foo.fooDate attribute

    2. Transform it using the code above (or similar) and assign the NSDate result to foo.fooDateTransient

    3. Use fooDateTransient as your sectionNameKeyPath when creating the fetchedResultsController object.

    PS: I haven't tested this myself but should be worth a shot!

    Good luck, Rog

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

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