0

I'm piggy backing off this previous question that was never answered - Date Section Titles don't work

Can anyone provide code to help me (and the community) properly convert Apple's project DateSectionTitles (http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html) from Month section titles to Day section titles?

From my numerous attempts, I believe it fundamentally comes down to altering two parts:

(1) RootViewController.m's titleForHeaderInSection

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> theSection = [[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 NSArray *monthSymbols = nil;

    if (!monthSymbols) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setCalendar:[NSCalendar currentCalendar]];
        monthSymbols = [[formatter monthSymbols] retain];
        [formatter release];
    } 

    NSInteger numericSection = [[theSection name] integerValue];

    NSInteger year = numericSection / 1000;
    NSInteger month = numericSection - (year * 1000);

    NSString *titleString = [NSString stringWithFormat:@"%@ %d", [monthSymbols objectAtIndex:month-1], year];

    return titleString;

}

(2) and Event.m's sectionIdentifier

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

Thank you in advance!

Community
  • 1
  • 1
user1424344
  • 5
  • 1
  • 2

1 Answers1

0

Haven't tried it myself, but here it goes,

Change this,

NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[self timeStamp]];

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

to this,

NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:[self timeStamp]];

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

It should give you a unique sectionIdentifier for every day, instead of every month.

The reason you get sections as months, is because every occurrence has a section identifier calculated as,

([components year] * 1000) + [components month]]

Any day in a given month, will have the same section identifier.

2009/08/01

e.g. (2009 * 1000) + 8;

By going further,

([components year] * 1000) + ([components month] *100) + [components day]

you should get, (2009 * 1000) + (8 * 100) + 1 = 2009801

Giving you a unique identifier, for each day. Instead of each month.

Hope it helps.

nmdias
  • 3,888
  • 5
  • 36
  • 59
  • nice! ok that is getting really close... Now it is a matter of 1) reformatting the Title String and 2) Displaying a Section Header for Each Day. I added a NSInteger for *day as: NSInteger day = numericSection - (month * 100); and the String as: NSString *titleString = [NSString stringWithFormat:@"%@ %d", [monthSymbols objectAtIndex:month-1], day]; and I'm returning: "January 2011901 1" Any thoughts on changing the following to finish what we started? Thanks – user1424344 Sep 07 '12 at 15:42
  • It would be a good idea to implement - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath then get the object that refers to that particular row. Something like this should do it, (YourObjectTypeHere *)[[self fetchedResultsController] objectAtIndexPath:indexPath]. Your object should have a property of type date. Your section identifier has the purpose of identifying the section for a given object. Not be used to determine it's contents. – nmdias Sep 08 '12 at 22:37