3

我正在构建一个将用作日常阅读指南的应用程序。数据全部存储在一个 XML 中,该 XML 将存储在应用程序中,并根据 pubDate 进行排序。关于每个部分代码中的行数,如果我只输入一个数字,我会得到错误,但如果我输入

[array count];

它显示了每一个项目。我能得到一些关于如何实现我的目标的建议吗?

编辑:这是我的应用程序的更多代码。我使用 ASIHTTPRequest 和 GDataXML 来解析 XML 并将每个项目存储在一个数组中。我想要做的是只显示最早的条目第 1 天,添加第二天的第 2 天,依此类推。如果我在 numberOfRowsInSection 中输入除数组计数之外的任何其他数字,它就会崩溃。我相信这是由于用于按日期对数组条目进行排序的代码。

- (void)requestFinished:(ASIHTTPRequest *)request {

    [_queue addOperationWithBlock:^{

        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData] 
                                                               options:0 error:&error];
        if (doc == nil) { 
            NSLog(@"Failed to parse %@", request.url);
        } else {

            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];                

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries) {

                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                        RSSEntry *entry1 = (RSSEntry *) a;
                        RSSEntry *entry2 = (RSSEntry *) b;
                        return [entry1.articleDate compare:entry2.articleDate];
                    }];

                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

                }                            

            }];

        }        
    }];
    [self.refreshControl endRefreshing];

}

如何将其更改为按日期排序,在第一天显示最早的,并每天添加一个?

4

4 回答 4

4

我会将日期值存储到您的 NSUserDefaults 中并使用它来比较并查看日期是否已更改,然后使用它来修改该部分中的行数。我已经对这段代码进行了大量评论,希望它能更好地解释。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    int numberOfDays = 0;   //initialize integer variable    

    //get the current date from the user's device
    NSDate *now = [NSDate date];

    //create a dateformatter to handle string conversion
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    // String to store in defaults.
    NSString *todaysDateString = [dateFormatter stringFromDate:now];

    // get access to the user defaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults valueForKey:@"date_last_opened"]) {

        // if there is no value for the key, save today's date as the string we formatted
        [defaults setValue:todaysDateString forKey:@"date_last_opened"];

    } else {

        // there is already a value for date-last-opened, so pull it from the defaults, and convert it from a string back into a date.
        NSDate *dateLastOpened = [dateFormatter dateFromString:[defaults valueForKey:@"date_last_opened"]];

        if ([dateLastOpened compare:now] == NSOrderedAscending) {

            // if the date_last_opened is before todays date, get the number of days difference.

            unsigned int unitFlags = NSDayCalendarUnit;
            NSDateComponents *comps = [calendar components:unitFlags fromDate:dateLastOpened  toDate:now options:0];

            numberOfDays = [defaults integerForKey:@"totalDays"] + [comps day];
        [defaults setInteger:numberOfDays forKey:@"totalDays"];
        }
    }

return numberOfDays;

}

编辑:此代码假定,当应用程序首次启动时,您已经将类似于 @"totalDays" 的键的 NSUserDefault 值设置为 1,例如在 viewDidLoad 中可能:

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

        // if there is no value for the key, set it to 1
        [defaults setInteger:1 forKey:@"totalDays"];

    }
于 2012-12-05T00:15:38.407 回答
2

在您的课程 AppDelegate.m 中,您可以这样做:

//Application did launch
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}

//The application was in background and become active
- (void)applicationWillEnterForeground:(UIApplication *)application
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}

然后使用 NSUserDefault Key @"LaunchCount" 你可以添加一个 Table-Row。

于 2012-12-22T05:34:06.187 回答
0

NSTimeInterval像24小时这样的东西怎么样 。

于 2012-12-24T02:40:29.083 回答
0

听起来您想要一个带有 [array count] 行的部分,而现在您要返回 [array count] 部分和 [array count] 行。

于 2012-12-04T22:58:49.287 回答