我通过JSON解析了几个东西到一个表视图,但我有问题,解析需要大约一秒(我知道这是由于网络指标),但然后有一个巨大的延迟,直到数据出现在表格视图中。UITableView重新加载数据需要永久
我试图放置[tableView reloadData];在几个地方已经但没有成功。
这是我的代码。
我将mainThreadQueue和myClassicoAPI定义为一个宏。
- (void)viewDidLoad
{
[super viewDidLoad];
arrayNeuheiten = [[NSArray alloc] init];
arrayArtikelName = [[NSArray alloc] init];
dictionaryNewStuff = [[NSDictionary alloc] init];
[self parseJSONWithURL:myClassicoAPI];
//[self performSelector:@selector(updateTableView) withObject:nil afterDelay:NO];
[neuheietenTable reloadData];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[neuheietenTable reloadData];
}
-(void) updateTableView {
[neuheietenTable reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return (self.arrayNeuheiten.count<17)?self.arrayNeuheiten.count : 17;
return MIN(18, arrayNeuheiten.count);
}
-(void) parseJSONWithURL: (NSURL *) jsonURL {
dispatch_async(mainThreadQueue, ^{
NSError *error = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *json =[NSString stringWithContentsOfURL:jsonURL encoding:NSJSONWritingPrettyPrinted error:&error];
if (error == nil) {
NSData *jsonData = [json dataUsingEncoding:NSJSONWritingPrettyPrinted];
dictionaryNewStuff = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
if (error == nil) {
dispatch_async(mainThreadQueue, ^{
arrayArtikelName = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"];
arrayNeuheiten = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"];
[neuheietenTable reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
} else {
nil;
}
} else {
nil;
}
});
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NeuheitenCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[arrayArtikelName objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}
在此先感谢
康斯坦丁
调用这样的主线程表格重载方法,[自performSelectorOnMainThread:@selector(reloadData)withObject:无waitUntilDone:YES]; – karthika
如果我直接调用reloadData,会出现错误。你的意思是我的无效或从字面上reloadData? –
[self.tableView performSelectorOnMainThread:@selector(reloadData)withObject:self waitUntilDone:YES]; – karthika