Day for sectionIdentifier for Custom Section Titles

58 Views Asked by At

On Apple developer docs, there is Custom Section Titles (https://developer.apple.com/library/ios/samplecode/DateSectionTitles/Introduction/Intro.html) where it obtains the year and month.

I would like to grab the day so I can build a section title that has the day, month and year.

How would I grab the day from this code?

code:

/*
     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];

From the

1

There are 1 best solutions below

0
On BEST ANSWER

Here in the above posted code It is a section info that you are getting.

And section means name of the Month and Year only, because you are fetching here only Month and Year, you are not fetching the date so, you will not have Day.

- (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;
}    

Yew but through "APIEvent" and time stamp you can have day components..