2012-12-17 106 views
0

我想先取一個已經構建的xml,解析它,然後在xml中只添加最舊的項目,並且每天添加1個。用於解析的代碼是:每天從xml添加1項到tableview

- (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]; 

     }  
    } 

} 

我怎樣才能改變這種做法,它只會增加對首批推出的應用程序的最古老的項目,等等?

+0

這難道不是你的另一個問題嗎? http://stackoverflow.com/questions/13713471/add-one-row-to-tableview-each-day-app-used – rmaddy

+0

@HotLicks我已經嘗試在numberofrowsinsection限制爲1,並寫了一些代碼,每個添加1但它會使索引0超出空數組消息範圍的應用程序崩潰。 – user717452

+0

然後它聽起來像你沒有添加到數組中。忘記tableview一段時間,只是想出如何添加到NSMutableArray每一天。 –

回答

0

在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]; 

這個代碼最後一位希望能做些什麼原本希望的。