如何使用核心数据将udview与nsdate分类为今天,本周,下周等部分

时间:2011-11-22 06:55:36

标签: iphone uitableview core-data nsdate

我可以使用排序描述符按照开始日期的顺序对表格视图进行排序,但我想将已排序的tableview分组为一个部分中包含今天日期的部分,下一周的日期在另一部分中,等等。

我发现了这篇帖子get NSDate today, yesterday, this Week, last Week, this Month, last Month... variables,可让我计算事件的日期。

Apple有一个示例代码排序日期为年,http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html

我无法找到将日期组织到这些部分的方法。如果有人知道这样做的帖子或示例代码,请告诉我。发布示例代码也将受到赞赏。

这就是我想要实现的目标。 enter image description here

编辑: 我还没弄清楚到底是怎么做到这一点的,但到目前为止我还会发布进展情况。

我现在可以准确地计算本周,下周等的日期。

这是执行此操作的代码:

  NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
[cal setLocale:[NSLocale currentLocale]];

NSDateComponents *nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setHour:0]; //8a.m.
[nowComponents setMinute:0];
[nowComponents setSecond:0];


nowComponents = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:today];
[nowComponents setMonth: [nowComponents month]]; //This Month begins on


NSDate *thisMonthB = [cal dateFromComponents:nowComponents];

nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week] - 1]; //Last week began on

NSDate *lWeek = [cal dateFromComponents:nowComponents];

nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week]]; //This Week begins on

NSDate *tWeekB = [cal dateFromComponents:nowComponents];


nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: 0];  // Yesterday
NSDate *yesterday = [cal dateFromComponents:nowComponents];



nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: 1]; // Today
NSDate *tday = [cal dateFromComponents:nowComponents];


nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: +2]; // Tomorrow
NSDate *tom = [cal dateFromComponents:nowComponents];

nowComponents = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[nowComponents setDay: +3]; // Tomorrow ends on
NSDate *tomE = [cal dateFromComponents:nowComponents];


nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week] + 1]; //This Week ends on

NSDate *tWeekE = [cal dateFromComponents:nowComponents];



nowComponents = [cal components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:today];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week] + 2]; //Next week end on


NSDate *nWeekE = [cal dateFromComponents:nowComponents];

nowComponents = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:today];
[nowComponents setMonth:[nowComponents month] + 1];//This Month ends on


NSDate *thisMonthE = [cal dateFromComponents:nowComponents];





NSLog(@"this month began on =%@",[NSDateFormatter localizedStringFromDate:thisMonthB dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"last week began on =%@",[NSDateFormatter localizedStringFromDate:lWeek dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"this week begin on =%@",[NSDateFormatter localizedStringFromDate:tWeekB dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"yesterday=%@",[NSDateFormatter localizedStringFromDate:yesterday dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );

NSLog(@"today=%@", [NSDateFormatter localizedStringFromDate:tday dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle]);
NSLog(@"tomorrow=%@",[NSDateFormatter localizedStringFromDate:tom dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"tomorrow ends on =%@",[NSDateFormatter localizedStringFromDate:tomE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"this Week ends on =%@",[NSDateFormatter localizedStringFromDate:tWeekE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"next Week ends on =%@",[NSDateFormatter localizedStringFromDate:nWeekE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );
NSLog(@"this Month ends on=%@",[NSDateFormatter localizedStringFromDate:thisMonthE dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle] );

它与我之前发布的链接上的代码略有不同。此代码更加可靠和准确。

现在对于节头,如果在核心数据对象中创建属性,例如带有NSString属性的sectionTitle,然后创建一个nsmanagedobject子类,则可以为该属性创建一个自定义的getter,如

.m文件

  • (NSString *)sectionTitle {

    返回@“今天”; }

并且在获取的结果控制器中,在获取时您可以选择为获取的结果控制器放置此属性以便为您放置这些部分,如下所示:

 [[NSFetchedResultsController alloc] 
 initWithFetchRequest:fetchRequest
 managedObjectContext:[self managedObjectContext]
 sectionNameKeyPath:@"sectionIdentifier"
 cacheName:@"cache"];

你会看到那里的部分。

问题是表视图方法,节中的行数和节中标题的标题。我无法弄清楚如何将获取的结果放入表视图中定义的部分。

如果有人可以提供帮助,请这样做。我迷失在如何实现这一点,似乎没有任何帮助或解决方案,如何做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以使用Erica Sandun的NSDate + Utilies类。这将有很大帮助。