-1

The apple sample project DateSectionTitles demonstrates how to group sections by year and month, how to extends it to support year month day and weekday?

sectionIdentifier is a transient attribute derives from persistent date attribute timestamp.

- (NSString *)sectionIdentifier {

    // Create and cache the section identifier on demand.

    [self willAccessValueForKey:@"sectionIdentifier"];
    NSString *tmp = [self primitiveSectionIdentifier];
    [self didAccessValueForKey:@"sectionIdentifier"];

    if (!tmp) {
        /*
         Sections are organized by month and year. Create the section identifier
         as a string representing the number (year * 1000 + month);
         this way they will be correctly ordered chronologically regardless of
         the actual name of the month.
         */
        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit)
                               fromDate:[self timeStamp]];
        tmp = [NSString stringWithFormat:@"%d", [components year] * 1000 + [components month]];
        [self setPrimitiveSectionIdentifier:tmp];
    }
    return tmp;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];

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

    return titleString;
}

I don't know the exact algorithm to calculate year month day and weekday like below code.

tmp = [NSString stringWithFormat:@"%d", [components year] * 1000 + [components month]];

And the calculate for the data source method.

id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];

NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);
Andy_24
  • 1,353
  • 2
  • 12
  • 20

1 Answers1

1

You need an "algorithm" that creates a different result for each day (weekday is the same as day. There is no "Thursday, 13 March 2015").

Let's say you have the 13th of March 2015. A good unique string for this would be 20150313. Or year * 10000 + month * 100 + day.

e.g.:

NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                               fromDate:[self timeStamp]];
tmp = [NSString stringWithFormat:@"%d", [components year] * 10000 + [components month] * 100 + [components day]];

Then invert the calculations to get back the individual components:

NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 10000;
NSInteger month = (numericSection - (year * 10000))/100;
NSInteger day = (numericSection - (year * 10000 + month * 100))

Or just get the NSDate from one of the objects in the section:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> theSection;// = [[self.fetchedResultsController sections] objectAtIndex:section];
    static NSDateFormatter *formatter = nil;

    if (!formatter)
    {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setCalendar:[NSCalendar currentCalendar]];

        NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMMM yyyy dd EEEE" options:0 locale:[NSLocale currentLocale]];
        [formatter setDateFormat:formatTemplate];
    }

    APLEvent *event = [[theSection objects] firstObject];
    if (event) {
        NSDate *date = [event timeStamp];
        return [formatter stringFromDate:date];
    }
    return nil;
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247