【发布时间】:2015-07-28 23:02:36
【问题描述】:
我想知道是否有人可以帮助我。我试图仅按月或年而不是按天对日期记录进行分组。我感谢您的帮助。
`(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {
id theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
/* 节信息来自事件的节标识符,它是一个字符串,表示数字(年 * 1000)+ 月。要显示部分标题,请将年份和月份组件转换为字符串表示形式。 */
static NSDateFormatter *formatter = nil;
if (!formatter) { formatter = [[NSDateFormatter alloc] init]; [formatter setCalendar:[NSCalendar currentCalendar]];
NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMM YYYY" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:formatTemplate];
}
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 10000;
NSInteger month = (numericSection - (year * 10000))/100;
NSInteger day = (numericSection - (year * 10000 + month * 100));
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = year;
dateComponents.month = month;
dateComponents.day = day;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
NSString *titleString = [formatter stringFromDate:date];
return titleString;
}`
【问题讨论】:
标签: objective-c