【问题标题】:Use of NSDate to get dates使用 NSDate 获取日期
【发布时间】:2010-08-21 20:54:19
【问题描述】:

我尝试了很多方法,但始终失败,希望您能帮助我实现我想做的事情。

我正在制作一个 iPad 应用程序,我将在一个视图中有五个表格,每个表格都有一个日期作为这种格式的标题/标题,例如星期一 20

这五张桌子是周一到周五。这是我做不到的一点。我想计算出当前日期,然后突出显示今天的表格,显然每天都在变化。

例如,假设今天是 9 号星期四。星期四表格被突出显示,然后自动将其他表格的日期设置为星期四左右。

想一想学校的时间表/计划者/日记。周一到周五,每个都标有日期。

编辑:那如果我这样做呢?如果我将此添加到您给我的代码中,如果 TRUE(按下按钮)添加 7 天,就像在示例表单 Apple.xml 中一样完成。但是我的问题,什么是公历?我用什么代替它?我已经看到它在 Apple 的日历样本中大量使用。

if (tableView == monTable){
        if(next == TRUE){
            [comps setDay:7];
            NSDate *date = [gregorian dateByAddingComponents:comps toDate:curDate  options:0];
        }
        else{
            [comps setWeekday:2];
        }
    }

【问题讨论】:

    标签: ios iphone uitableview date nsdate


    【解决方案1】:

    您可以通过这种方式获取周一到周五的日期:

    NSDate* curDate = [NSDate date]; // Get current date
    NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
    NSDateComponents* 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.
    for (int i = 2; i <= 6; i++){ 
        [comps setWeekday:i];
        NSDate *tDate = [calendar dateFromComponents:comps];
        NSLog(@"%@", tDate);
    }
    

    要确定要突出显示的日期(当前日期),您只需检查日期的工作日部分。

    编辑: titleForHeaderInSection 方法可能如下所示:

    - (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section{
        NSDate* curDate = [NSDate date]; // Get current date
        NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
        NSDateComponents* 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)
            [comps setWeekday:2];
        if (tableView == tueTable)
            [comps setWeekday:3];
        if (tableView == wedTable)
            [comps setWeekday:4];
        if (tableView == thuTable)
            [comps setWeekday:5];
        if (tableView == friTable)
            [comps setWeekday:6];
    
        NSDate *tDate = [calendar dateFromComponents:comps];
        NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setDateFormat:@"EEE, MMM d"];
    
    
        return [formatter stringFromDate:tDate];
    }
    

    【讨论】:

    • 啊,好吧,但是如果我使用您在 titleForHeaderInSection 中提供的代码,我如何让它遍历每个表并给出正确的日期?例如,我有 monTable、tueTable、wedTable 等。但是如何让它将正确的日期分配给正确的表?
    • 例如,您可以为表格设置标签属性(例如,从 1 到 5)并像这样设置工作日:[comps setWeekday:1 + table.tag];,或者只是 'it (table == monTable) [comps setWeekday:2];等每个表。
    • 嗯,谢谢你的帮助,这一切都说得通,但是当我添加这些 if 语句时,我的应用程序崩溃了。我将您的代码放在 titleForHeaderInSection 中,并将这些语句添加到 for 循环中。 f(tableView == monTable) [comps setWeekday:2]; if(tableView == tueTable) [comps setWeekday:3]; if(tableView == wedTable) [comps setWeekday:4]; if(tableView == thuTable) [comps setWeekday:5]; if(tableView == friTable) [comps setWeekday:6];另外,我不需要“返回”这些 if 语句吗?如果我尝试这样做,它会引发错误。
    • 崩溃时会出现什么错误?在这种情况下你不需要 for 循环 - 只需用你的 if 语句代替它
    • 在那些 if 语句中,您只需要设置适当的工作日组件值。之后(毕竟是 if 语句)- 从 comps 创建 nsdate 对象,是的,您需要将日期格式化为适当的字符串值(使用 NSDateFormatter)并返回该字符串。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2011-08-07
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多