开发者

Get Date from Week number with section headers number being the week

OK... so I'm trying to use a UITableView and have it sectioned off into 52 sections.. for 52 weeks in a year... each section has 7 days (cells) for the week. I want to have each sectionHeader say "Week of 'month day, year'". Every time I try to get the string to print in the headerSection of the corresponding week nothing shows up. Is there any way to use the header section number to set the first day of that weeks date into the sectionHeader? below is the code that I have been trying to get to work... It seems like it makes sense. But I'm doing something wrong. I would really appreciate any help. Thank you in advance.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSDate *now = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
    [comp setWeek:section];
    [com开发者_开发知识库p setDay:1];
    NSDate *resultDate = [gregorian dateFromComponents:comp];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE, MMM d, ''yy"];

    NSString *myDateString = [formatter stringFromDate:resultDate];

    NSString *sectionHeader = nil;

    switch (section) {
    case 0:
        sectionHeader = myDateString;
        break;
    case 1: 
        sectionHeader = myDateString;
        break;
    case 2:
        sectionHeader = myDateString;
        break;
    default:
        break;
    }   

    return sectionHeader;
}


I'm not sure why you decided to use a switch statement in your code, but it serves absolutely no purpose and is causing at least some of your problems. For all but three weeks (the three cases covered in your switch statement), your titleForHeaderSection: method will return nil. Instead, you should try something like this:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSDate *now = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];

    [comp setWeek:(section + 1)]; // section numbers start at 0, so need to add 1
    [comp setWeekDay:1]; // Note that this is very different than setting day to 1

    NSDate *resultDate = [gregorian dateFromComponents:comp];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMM d, yyyy"];

    NSString *myDateString = [formatter stringFromDate:resultDate];

    [gregorian release]; // Clean up allocated objects by releasing them
    [formatter release];

    return [NSString stringWithFormat:@"Week of %@", myDateString];
}

Note that I set the weekday above and not the day, since I believe you are trying to get the date of the first day of that week.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜