在Apple开发人员文档中,有自定义章节标题(https://developer.apple.com/library/ios/samplecode/DateSectionTitles/Introduction/Intro.html),用于获取年份和月份。
我想抓住这一天所以我可以建立一个包含日,月和年的章节标题。
我如何从这段代码中获取这一天?
代码:
/*
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];
来自
答案 0 :(得分:1)
以上发布的代码这是您要获得的部分信息。
部分仅表示月份和年份的名称,因为您 仅在此处获取月份和年份,因此您不会获取日期,因此您将没有日期。 强>
- (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;
}
但是通过“APIEvent”和时间戳你可以有日间组件..