【问题标题】:Pull NSDates from plist and only display today's and tomorrow's events in tableview从 plist 中提取 NSDates 并仅在 tableview 中显示今天和明天的事件
【发布时间】:2011-11-16 20:58:45
【问题描述】:

在一个 plist 中,我在一个数组中有几个字典(事件,例如足球比赛),如下所示:

<dict>
    <key>date</key>
    <date>2011-11-24T00:00:00Z</date>
    <key>title</key>
    <string>Amsterdam - Roosendaal</string>
</dict>
<dict>
    <key>date</key>
    <date>2012-06-25T00:00:00Z</date>
    <key>title</key>
    <string>Rotterdam - Utrecht</string>
</dict>

我有一个表格视图,其中有名为“今天”和“明天”的部分。现在,每当今天发生事件时,我都希望在“今天”部分看到它,而当活动计划在明天发生时,我希望在“明天”部分看到它。

编辑:我现在有这个代码:

self.Array = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"matches" ofType:@"plist"]];

for (int i=0; i<[Array count]; i++) {
        NSDate *datum = [[Array objectAtIndex:i] objectForKey:@"datum"];
        NSCalendar *cal = [NSCalendar currentCalendar];
        NSDateComponents *components = [cal components:       (NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
        NSDate *today = [cal dateFromComponents:components];
        components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:datum];
        NSDate *otherDate = [cal dateFromComponents:components];

        if([today isEqualToDate:otherDate]) {

        //do stuff

        NSLog(@"%i, %@", i, otherDate);
        NSLog(@"Match is today");

    }
    else {
        NSLog(@"Match is not today");
    }

从 NSLog 看来,它以正确的方式进行了比较。但是,我现在想在一个部分中显示那些具有今天日期的字典。我想我因此必须创建一个数组(也许通过在“//do stuff”区域添加东西,例如addObject?)对此有什么想法吗?

【问题讨论】:

    标签: iphone objective-c ios xcode


    【解决方案1】:

    您还应该研究 NSCalendar 和 NSDateComponents。您可以使用它们来获取日期(或日+月+年,或时代中的绝对日)作为日期所在的整数。这样,您就可以在一个日历日的边界上将两次比较为同一天。 (它也会为您考虑夏令时和其他奇怪的情况)。

    您还可以使用它们将当前日期([NSDate date])转换为当天开始的日期。示例在类的苹果文档中。

    [编辑] 在你拥有那部分之后(听起来像你做的那样)然后你说你想在一个带有部分的表格中显示它。基本上在同一天的测试之后想要设置一个二维数组(更具体地说,一个带有 2 个对象的 NSArray,这些对象本身就是 NSMutableArrays)。您向其中一个 NSMutableArrays 添加今天的对象,另一个向其中添加明天的对象。

    完成后,您可以填写 tableview 委托和数据源方法来填充表格。 (例如,节数将是您的外部数组的计数,每个单元格的索引路径将对应于外部数组(节)和内部可变数组(行)。)有关更多详细信息,请使用 TableView 编程指南。

    【讨论】:

    • 我已经用我现在遇到的代码编辑了这篇文章。你可能对此有一些想法。感谢您的回复。
    • 我在发布的答案中添加了更多内容。此外,查看您的代码,将尽可能多的日历设置和今天的日期移动到您的 for 循环之前,而不是像现在这样在内部会更有效。某些日期操作可能很昂贵。
    • 谢谢!听起来很清楚。由于我缺乏知识,因此将查看这些数组的文档。查看 addObject 是一个好的起点吗?再次感谢,帮助很大。
    • addObject 确实是将对象添加到内部数组所需要的。通过调用 NSArray 的方法 'arrayWithObjects' 创建外部数组时,可以将两个内部数组添加到外部数组中。再一次,如果您能掌握 Tableview 编程指南中的所有内容,那么您将处于良好状态。祝你好运!
    【解决方案2】:

    首先,不要与字符串进行比较,保持数据为NSDate形式。

    那就试试吧

     if ( [myDate timeIntervalSinceDate:dateOfEvent] < 86400 ) { // less than a day apart.
     }
    

    (使用 [myDate isEqualToDate:dateOfEvent] 进行精确比较(到第二个),所以不要使用它)。

    您必须对其进行微调以确定“一天”实际上是什么,您需要找到一天的界限,但这应该不会太难。

    如果您想向用户显示日期,请使用NSDateFormatter

    您还可以使用 NSDateFormatter 将这些仅转换为日期(无时间戳),然后比较这些字符串,例如,您可以将 2011/16/11 与 2011/15/11 进行比较。 像这样:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setTimeStyle:NSDateFormatterNoStyle];
    NSDate *today = [NSDate date];
    NSDate *tomorrow = [today dateByAddingTimeInterval:86400];
    NSString *eventDateString = @"2011-11-24T00:00:00Z";    // get your date string from the array
    [df setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; // this matches your dictionary's format
    
    NSDate *eventDate = [df dateFromString:eventDateString];
    
    // now you have 3 dates.  convert them to timeless strings using another date formatter.
    NSDateFormatter *timeless = [[NSDateFormatter alloc] init];
    [timeless setTimeStyle:NSDateFormatterNoStyle];
    [timeless setDateStyle:NSDateFormatterMediumStyle];
    
    NSString *todayString    = [timeless stringFromDate:today];
    NSString *tomorrowString = [timeless stringFromDate:tomorrow];
    eventDateString          = [timeless stringFromDate:eventDate];
    if ( [todayString isEqualToString:eventDateString ] ) {
        NSLog(@" today %@  == event: %@", todayString, eventDateString);
    } else if ( [tomorrowString isEqualToString:eventDateString] ) {
        NSLog(@" tomorrow %@  == event: %@", tomorrowString, eventDateString);
    }
    [df release];
    [timeless release];
    

    【讨论】:

    • 感谢您的快速回复!这对我来说很有意义,并将尽快实施。但是,我不打算显示实际日期,而只显示右侧部分中的“标题”。我可能对你要求很高,但是对于在“if”语句中包含什么代码来实现这一点,或者继续我的旅程的方向有什么想法吗?
    • 我现在使用[today isEqualToDate:eventDate],这似乎有效。但是,我仍然不知道如果确实相等,事件的标题如何在 tableview 的“今天”部分中显示给用户。
    • 如何显示标题是一个完全不同的问题,您应该阅读 UITableViewCell 和 cellForRowAtIndexPath:,以及查看一些显示表格的示例项目。
    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 2016-05-28
    相关资源
    最近更新 更多