自定义部分标题的部分标识符的日期

Day for sectionIdentifier for Custom Section Titles

在 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];

来自

在上面发布的代码中,这是您正在获取的部分信息。

section 仅表示月份和年份的名称,因为您 此处仅获取月份和年份,您没有获取日期,因此不会有日期。

- (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" 和时间戳你可以有日期组件..