我正在构建一个将用作日常阅读指南的应用程序。数据全部存储在XML中,存储在app中,并根据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];
}
如何更改此选项以按日期排序,在第一天显示最早的一个,并每天添加一个?
答案 0 :(得分: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"];
}
答案 1 :(得分: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”,您可以添加一个表行。
答案 2 :(得分:0)
听起来你想要一个带[array count]行的部分,而现在你要返回[array count]部分和[array count]行。
答案 3 :(得分:0)
如24小时的NSTimeInterval
之类的东西。