【发布时间】:2013-10-21 07:54:30
【问题描述】:
我有许多 Game 对象,每个对象都有一个 date 参数。
在我的控制器中,我想获取这些游戏,按日期的month-year 组合分组 - 这样我可以获得月份列表,并且每个月 - 发生在那个月。
是否有一个简单的解决方案,无需修改我的 CoreData 模型?
【问题讨论】:
标签: iphone database core-data magicalrecord
我有许多 Game 对象,每个对象都有一个 date 参数。
在我的控制器中,我想获取这些游戏,按日期的month-year 组合分组 - 这样我可以获得月份列表,并且每个月 - 发生在那个月。
是否有一个简单的解决方案,无需修改我的 CoreData 模型?
【问题讨论】:
标签: iphone database core-data magicalrecord
Custom Section Titles with NSFetchedResultsController - 这个示例项目展示了如何做到这一点(如果我理解正确的话)。很明显,它没有使用 MagicalRecord,但应该很容易移植到 MR。
【讨论】:
这对我有用
在你的
model.h
添加
-(NSString *)yearMonth;
和
model.m
-(NSString *)yearMonth
{
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyy-MM"];
NSString *formattedDateString = [dateFormatter stringFromDate:self.eventDate];
return formattedDateString;
}
然后像这样按组调用 MR_Fetch 方法:
NSFetchedResultsController *frc = [model MR_fetchAllGroupedBy:@"yearMonth" withPredicate:nil sortedBy:@"eventDate" ascending:NO];
【讨论】: