2015-03-13 69 views
-1

蘋果示例項目DateSectionTitles演示瞭如何按年份和月份對各部分進行分組,以及如何將它擴展爲支持年月日和星期幾?如何從年月日星期幾之和中提取日期分量乘以iOS中的某個整數?

sectionIdentifier是源自持續日期屬性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; 
} 

我不知道計算年月日和星期幾的確切算法,如下面的代碼。

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

並計算出數據源方法。

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

NSInteger numericSection = [[theSection name] integerValue]; 
NSInteger year = numericSection/1000; 
NSInteger month = numericSection - (year * 1000); 

回答

1

你需要一個「算法」,對於每天創建一個不同的結果(平日是一樣的一天。沒有「週四,13 2015年3月」)。

比方說,你有2015年3月13日。一個好的唯一字符串是20150313.或year * 10000 + month * 100 + day

例如爲:

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

然後反轉計算找回各個組件:

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

或者從節中的對象的一個​​剛剛拿到的NSDate:

- (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; 
} 
相關問題