2010-08-23 5 views
0

내 응용 프로그램에 다음 코드가 있습니다. MonTable, tueTable 등의 단일보기에 5 개의 표가있는 iPad 앱입니다.이 표는 월요일을 금요일로 나타냅니다.NSDate/NSCalendar 문제

이 코드에서 나는 날짜를 얻고 각 테이블 제목을 금요일 (금주)에 월요일로 설정합니다. 다음 버튼을 누르면 nextWeek이 TRUE가되고 테이블 데이터가 다시로드됩니다. 이것은 그 다음에 그 주가 증가했다는 것을 의미합니다. 만나다?

-(IBAction)nextWeekDown{ 
    nextWeek = TRUE; 
    [monTable reloadData]; 
} 

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

    curDate = [NSDate date]; // Get current date 
    calendar = [NSCalendar currentCalendar];// Init calendar 
    comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components 

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one. 
    if (tableView == monTable){ 
     if(nextWeek == TRUE){ 
      [comps setHour:168]; 
      NSDate *date = [calendar dateByAddingComponents:comps toDate:curDate options:0]; 
     } 
     else{ 
      [comps setWeekday:2]; 
     } 
    } 
    if (tableView == tueTable){ 
     if(nextWeek == TRUE){ 
      [comps setHour:168]; 
      NSDate *date = [calendar dateByAddingComponents:comps toDate:curDate options:0]; 
     } 
     else{ 
      [comps setWeekday:3]; 
     } 
    } 
    if (tableView == wedTable){ 
     if(nextWeek == TRUE){ 
      [comps setHour:168]; 
      NSDate *date = [calendar dateByAddingComponents:comps toDate:curDate options:0]; 
     } 
     else{ 
      [comps setWeekday:4]; 
     } 
    } 
    if (tableView == thuTable){ 
     if(nextWeek == TRUE){ 
      [comps setHour:168]; 
      NSDate *date = [calendar dateByAddingComponents:comps toDate:curDate options:0]; 
     } 
     else{ 
      [comps setWeekday:5]; 
     } 
    } 
    if (tableView == friTable){ 
     if(nextWeek == TRUE){ 
      [comps setHour:168]; 
      NSDate *date = [calendar dateByAddingComponents:comps toDate:curDate options:0]; 
     } 
     else{ 
      [comps setWeekday:6]; 
     } 
    } 

    NSDate *tDate = [calendar dateFromComponents:comps]; 
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [formatter setDateFormat:@"EEE, MMM d"]; 

    return [formatter stringFromDate:tDate]; 
} 

내 문제는 일부 유선 이유로는 버튼이 어떤 아이디어를 누를 때 변경 다음 주 다른 일 중에 칠일에 의해 월요일 증가한다는 것이다? 감사.

답변

0

nextWeekDown 메서드에서는 monTable 만 다시로드합니다. 자연스럽게 다른 테이블은 다시로드되지 않습니다. 당신은 다른 모든 테이블을 다시로드해야합니다 ...

+0

당신은 바보처럼 보이게되었습니다 ... :) Im half awake ever again. 그것도 내 자신의 코드를 읽는 데 효율적이라는 것을 배우는 부분이라면, 언젠가는 거기에 도착할 것입니다. –

+0

이 질문에 대한 후속 조치. 나는 버튼을 누를 때 nextWeek이 TRUE가됨을 의미하므로, 물론 테이블은 7 일 전에 다시로드됩니다. 그러나 내 인생에서 나는 다음 주일에 = 거짓말을하고 다시 버튼을 누를 수 없습니다. –

+0

걱정 마세요. 그러나 또 다른 문제! 제가 다음 주 버튼을 누르면 주를 지날 것입니다. 그러나 9 월 말에 끝나면 8 월로 돌아갑니다. –