【问题标题】:Add 1 item from xml to tableview per day每天将 1 个项目从 xml 添加到 tableview
【发布时间】:2012-12-04 23:43:03
【问题描述】:

我想获取一个已经构建的 xml,对其进行解析,首先只添加 xml 中最旧的项目,然后每天再添加 1 个。用来解析的代码是:

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {            

        NSString *blogTitle = [channel valueForChild:@"title"];                    

        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {

            NSString *articleTitle = [item valueForChild:@"title"];
            NSString *articleUrl = [item valueForChild:@"guid"];            
            NSString *articleDateString = [item valueForChild:@"pubdate"];        
            NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
            NSString *articleImage = [item valueForChild:@"description"];
            NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

            RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
                                                      articleTitle:articleTitle
                                                        articleUrl:articleUrl
                                                       articleDate:articleDate
                                                      articleImage:articleImage
                                                              date:thedate] autorelease];
            [entries addObject:entry];

        }      
    }

}

如何更改此设置,使其仅在首次启动应用时添加最旧的项目,等等?

【问题讨论】:

  • 这不是你的另一个问题的欺骗吗? stackoverflow.com/questions/13713471/…
  • @HotLicks 我已经尝试在 numberofrowsinsection 中限制为 1,并编写了一些代码以每天添加 1,但它会使索引为 0 的应用程序崩溃,超出空数组消息的范围。
  • 那么听起来你没有添加到数组中。暂时忘记表格视图,只需弄清楚如何每天添加到 NSMutableArray。
  • @HotLicks 没错,除了大多数表格视图教程都非常复杂,而且每个教程都是针对特定情况编写的。
  • 所以忘了tableview吧。一旦你有了要展示的项目数组,将它们放入 tableview 就很简单了。如果你把这两者混为一谈,你会把自己逼疯的。

标签: ios xcode uitableview


【解决方案1】:

在我放入的 AppDelegate didFinishLaunchingWithOptions 方法中:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults integerForKey:@"totalDays"]) {

        // if there is no value for the key, set it to 0
        [defaults setInteger:0 forKey:@"totalDays"];
        [defaults synchronize];
    }
    if (![defaults objectForKey:@"currentDate"]) {
        [defaults setObject:@"32 01" forKey:@"currentDate"];
        //this will ensure that no one's start date ever matches current value
        [defaults synchronize];
    }
    // get the current date
    NSDate *date = [NSDate date];

    // format it
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"dd MM"];

    // convert it to a string
    NSString *dateString = [dateFormat stringFromDate:date];

    // free up memory
    [dateFormat release];
    if ([defaults objectForKey:@"currentDate"]){
    NSString *checkdate = [defaults objectForKey:@"currentDate"];
    if (![dateString isEqualToString:checkdate]) {
       //now if your current date is different from the last date it will add 1 to the total days key
        NSInteger currentnumber = [defaults integerForKey:@"totalDays"];
        NSLog(@"The current number is %i", currentnumber);

        [defaults setObject:dateString forKey:@"currentDate"];
        [defaults setInteger:currentnumber+1 forKey:@"totalDays"];
        [defaults synchronize];
        NSLog(@"The new number is %i", [defaults integerForKey:@"totalDays"]);
    }
    }

然后在我添加的 NSPredicate 区域中:

NSDate *start = [NSDate dateFromRFC3339String:@"2013-01-01T00:00:00-06:00"];
            NSInteger difference = [[NSUserDefaults standardUserDefaults] integerForKey:@"totalDays"];

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"articleDate < %@",  [NSDate dateWithTimeInterval:(86400*difference) sinceDate:start]];
            NSArray *filteredArray = [entries filteredArrayUsingPredicate: predicate];

这最后一段代码有望实现最初的预期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多