我的应用程序中有以下代码。它是一个 iPad 应用程序,在一个名为 monTable、tueTable 等的单个视图中有五个表格。这些表格代表星期一到星期五。
在这段代码中,我获取日期并将每个表格标题设置为星期一到星期五(本周)的日期。然后,如果我按下按钮 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];
}
我的问题是,由于某种有线原因,它只会在星期一增加 7 天到下周,而当按下按钮时,其他日子都没有改变任何想法?谢谢。