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 {
}
什麼是最好的方式去做這件事?