2011-12-29 77 views
0

當我的tableview加載它從基於當前我們所在的星期從MySQL數據庫抓取數據。這一切工作正常,但我也希望用戶能夠點擊一個UIButton加載下週的數據。之後,按鈕停留在那裏,可以一遍又一遍地點擊以進入下一個星期。按下按鈕時,如何使用nscalendar&nsdate進入下一週?

NSDate *date = [NSDate date]; 
    NSCalendar *cal =[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 

    [cal setFirstWeekday:3]; // 


    NSDate *beginningOfWeek = nil; 
    [cal rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:nil forDate:date]; 
    NSDateComponents *beginComponents = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:beginningOfWeek]; 

    NSDateComponents *offset = [[NSDateComponents alloc]init]; 
    [offset setDay:6]; 
    [offset setHour:23]; 
    NSDate *endOfWeek = [cal dateByAddingComponents:offset toDate:beginningOfWeek options:0]; 
    NSDateComponents *endComponents = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:endOfWeek]; 

    NSTimeInterval tiStart = [beginningOfWeek timeIntervalSince1970]; 
    NSTimeInterval tiEnd = [endOfWeek timeIntervalSince1970]; 
    NSString *startWeekString = [NSString stringWithFormat:@"%f", tiStart]; 
    NSString *endWeekString = [NSString stringWithFormat:@"%f", tiEnd]; 


    NSURL *url = [NSURL URLWithString:@"http://localhost/test.php"]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    [request setPostValue:startWeekString forKey:@"startweek"]; 
    [request setPostValue:endWeekString forKey:@"endweek"]; 
    [request setDelegate:self]; 
    [request startAsynchronous]; 

} 

-(IBAction)nextWeek:(id)sender { 

} 

什麼是最好的方式去做這件事?

回答

0

我會將所有日曆和日期組件代碼重構爲一個方法,該方法需要NSDate(開始日期)和startWeekString和endWeekString作爲參數,並填充後兩個值。 在變量中跟蹤當前周(或當前使用的開始日期),並在nextWeek中將該變量增加一週,然後將其傳遞給日曆方法。

相關問題