部分日期标题 - 每天使用核心数据的部分

时间:2012-08-31 01:25:45

标签: objective-c ios core-data tableview sections

我小心翼翼地退回以前从未回答过的问题 - Date Section Titles don't work

任何人都可以提供代码帮助我(和社区)将Apple的项目DateSectionTitles(http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html)从Month部分标题正确转换为Day部分标题吗?

从我的无数次尝试中,我认为它从根本上归结为改变两个部分:

(1)RootViewController.m的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)和Event.m的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;
}

提前谢谢!

1 个答案:

答案 0 :(得分: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

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

进一步,

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

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

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

希望它有所帮助。