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!

4

1 回答 1

0

自己没试过,不过就这样

改变这个,

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

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

对此,

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

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

它应该为您每天提供一个唯一的 sectionIdentifier,而不是每个月。

您将部分作为月份的原因是因为每次出现都有一个部分标识符,计算如下:

([组件年份] * 1000) + [组件月份]]

给定月份中的任何一天都将具有相同的部分标识符。

2009/08/01

例如(2009 * 1000)+ 8;

通过更进一步,

([组件年] * 1000) + ([组件月] *100) + [组件日]

你应该得到 (2009 * 1000) + (8 * 100) + 1 = 2009801

每天为您提供唯一标识符。而不是每个月。

希望能帮助到你。

于 2012-09-04T23:12:20.837 回答