我在一个应用程序中有一个 viewController,它从 JSON 中检索数据,对其进行解析并填充到 UITableView 中。我正在使用线程加载数据,以便应用程序在检索数据时不会挂起。
问题:
numberOfRowsInSection 返回 0 并且 UITableView 有时在应用程序启动时不会被填充。虽然有时,一切正常。这都是随机的:S
可能的解释:
问题是,有时在检索数据之前调用 numberOfRowsInSection 。numberOfRowsInSection 返回名为“subjects”的 NSMutableArray 的计数值。调用 loadData 时会添加“subjects”中的对象。所以 numberOfRowsInSection 应该返回 'subjects' 的计数,并且不应在填充 'subjects' 后调用它。
有时当我启动应用程序时,在填充“主题”并且 UITableView 显示数据后调用 numberOfRowsInSection,但有时当我启动应用程序时,在填充“主题”之前调用 numberOfRowsInSection 并且 UITableView 不显示数据。
代码: 这是我的代码:
-(void)loadData:(id)sender
{
dispatch_queue_t getRemindersQueue=dispatch_queue_create("reminders JSON downloader with reload Button", NULL);
dispatch_async(getRemindersQueue, ^{
[self getReminders];
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationItem.rightBarButtonItem=sender;
[self.tableView reloadData];
});
});
dispatch_release(getRemindersQueue);
}
-(void)getReminders
{
NSURL * aURL = [NSURL URLWithString: @"http://www.merrycode.com/apps/IELTS/RemindersJSON"];
NSURLRequest *request = [NSURLRequest requestWithURL:aURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
NSError *responseError=nil;
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&responseError];
if(responseError)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *parsingError = [[UIAlertView alloc] initWithTitle:@"Network Error"
message:@"Can not reach the servers. Make sure you are connected to the internet."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[parsingError show];
});
return;
}
NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@" String of Reminders JSON: %@",str);
NSString *newStr= [self stringByRemovingControlCharacters:str];
response = [newStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonParsingError = nil;
NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSLog(@"%@", jsonParsingError);
NSLog(@" publicTimeline Array Count: %d", [publicTimeline count]);
if([publicTimeline count] == 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *parsingError = [[UIAlertView alloc] initWithTitle:@"Error Retriving Data"
message:@"There was an error reciving data from the server."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[parsingError show];
});
return;
}
NSDictionary *colleges;
for(int i=0; i<[publicTimeline count];i++)
{
colleges= [publicTimeline objectAtIndex:i];
NSLog(@"Reminders: %@", [colleges objectForKey:@"title"]);
[self.subjects addObject:[colleges objectForKey:@"title"]];
[self.dates addObject:[colleges objectForKey:@"date"]];
[self.description addObject:[colleges objectForKey:@"desc"]];
}
[self.subjectsInNSUserDefaults removeAllObjects];
[self.datesInNSUserDefaults removeAllObjects];
[self.descriptionInNSUserDefaults removeAllObjects];
[self.userDefaults setObject:self.subjects forKey:@"SUBJECTS"];
[self.userDefaults setObject:self.dates forKey:@"DATES"];
[self.userDefaults setObject:self.description forKey:@"DESCRIPTION"];
[self.userDefaults synchronize];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Array Count in numberOfRowsInSection: %d",[self.subjects count]);
return [self.subjects count];
}