我在过去和即将到来时将表格视图分组有点困难。这将由我的Core Data模型中的日期字段确定。所以所有项目的日期都是>今天即将到来,所有日期都是<今天将被视为过去。任何有关这方面的见解将不胜感激。
答案 0 :(得分:0)
您可以在实体类中使用此功能:
-(NSString*)dateToStringForSectionTitels{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *formattedDateString;
[dateFormatter locale];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:self.startDate];
NSDate *otherDate = [cal dateFromComponents:components];
if([today isEqualToDate:otherDate]) {
formattedDateString = @"Today";
}else{
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
formattedDateString = [dateFormatter stringFromDate:self.startDate];
}
NSTimeInterval distanceBetweenDates = [self.startDate timeIntervalSinceDate:[NSDate date]];
double secondsInAnMinute = 60;
NSInteger minutsBetweenDates = distanceBetweenDates / secondsInAnMinute;
if (minutsBetweenDates<30) {
formattedDateString = @"Starting Soon";
}else if(minutsBetweenDates<0){
formattedDateString = @"In the past";
}
return formattedDateString;
}
它会返回
然后将“dateToStringForSectionTitels”传递给你的获取结果控制器。
祝你好运。