每天使用的应用程序向TableView添加一行

我正在构建一个将用作日常阅读指南的应用程序。 数据全部存储在将存储在app中的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]; } 

如何更改此选项以按日期排序,在第一天显示最早的一个,并每天添加一个?

我会将一个日期值存储到您的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"]; } 

在您的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”,您可以添加表行。

听起来你想要一个带有[数组计数]行的部分,而现在你要返回[数组计数]部分和[数组计数]行。

如何像NSTimeInterval 24小时。