首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NSDate/NSCalendar问题

NSDate/NSCalendar问题
EN

Stack Overflow用户
提问于 2010-08-24 04:56:20
回答 1查看 680关注 0票数 0

我的应用程序中有以下代码。这是一个iPad应用程序,在一个名为monTable,tueTable等的单一视图中有五个表。这些表代表周一到周五。

在这段代码中,我获得了日期,并将每个表的标题设置为星期一到星期五(本周)的日期。然后,如果我按下一个按钮,nextWeek就会变为TRUE,并且我会重新装入表数据。这意味着一周的时间增加了。看见?

代码语言:javascript
运行
复制
-(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];
}

我的问题是,由于某种网络原因,它只增加了周一到下周的七天,而当按下按钮时,其他日子都没有改变,有什么想法吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-24 05:55:40

在nextWeekDown方法中,您只重新加载monTable -所以其他表自然不会重新加载。您还需要重新加载所有其他表...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3551634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档