Objective c 节日期自定义节标题的节标识符
在Apple developer文档中,有自定义的节标题(),可以从中获取年份和月份 我想抓住这一天,这样我就可以建立一个节标题,有天,月和年 我该如何从这段代码中抓住这一天 代码:Objective c 节日期自定义节标题的节标识符,objective-c,tableview,Objective C,Tableview,在Apple developer文档中,有自定义的节标题(),可以从中获取年份和月份 我想抓住这一天,这样我就可以建立一个节标题,有天,月和年 我该如何从这段代码中抓住这一天 代码: 从上面发布的代码中的中,您可以获得部分信息 节仅表示月份和年份的名称,因为您在这里只取月份和年份,而不取日期,所以您将没有日期。 但通过“APIEvent”和时间戳,您可以拥有日期组件 /* Section information derives from an event's sectionIdenti
从上面发布的代码中的中,您可以获得部分信息 节仅表示月份和年份的名称,因为您在这里只取月份和年份,而不取日期,所以您将没有日期。 但通过“APIEvent”和时间戳,您可以拥有日期组件
/*
Section information derives from an event's sectionIdentifier, which is a string representing the number (year * 1000) + month.
To display the section title, convert the year and month components to a string representation.
*/
static NSDateFormatter *formatter = nil;
if (!formatter)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMMM YYYY" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:formatTemplate];
}
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = year;
dateComponents.month = month;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
NSString *titleString = [formatter stringFromDate:date];
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"APLEvent" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Sort using the timeStamp property.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor ]];
// Use the sectionIdentifier property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"sectionIdentifier" cacheName:@"Root"];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}