2013-09-27 58 views
0

我通过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; 
} 

在此先感谢

康斯坦丁

+2

调用这样的主线程表格重载方法,[自performSelectorOnMainThread:@selector(reloadData)withObject:无waitUntilDone:YES]; – karthika

+0

如果我直接调用reloadData,会出现错误。你的意思是我的无效或从字面上reloadData? –

+1

[self.tableView performSelectorOnMainThread:@selector(reloadData)withObject:self waitUntilDone:YES]; – karthika

回答

2

你装上这是坏的mainthread的数据同步和阻塞的接口。 尝试使用NSURLConnection的加载你的JSON数据,并刷新你的UITableView在

(void)connectionDidFinishLoading:(NSURLConnection *)aConnection 

方法,你就大功告成了处理它的时候。

0

你好,我修改了一个公共网址和它的工作正常的代码。看其有用来解决问题的代码:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    arrayNeuheiten = [[NSArray alloc] init]; 
    arrayArtikelName = [[NSArray alloc] init]; 
    dictionaryNewStuff = [[NSDictionary alloc] init]; 

    [neuheietenTable reloadData]; 
    NSURL *url = [NSURL URLWithString:@"http://122.182.14.104:3004/build/product/15.json"]; 
    [self parseJSONWithURL:url]; 
    //[self performSelector:@selector(updateTableView) withObject:nil afterDelay:NO]; 

} 

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 
- (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 arrayArtikelName.count; 

} 



-(void) parseJSONWithURL: (NSURL *) jsonURL { 
    NSError *error = nil; 

    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) { 
      arrayArtikelName = [dictionaryNewStuff valueForKey:@"attributes"]; 
//   arrayNeuheiten = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"]; 
      [neuheietenTable reloadData]; 
      [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
     } 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; 
} 
+0

它什么也没做。从字面上看....?我的意思是你说它适用于你,但是如何? –

+0

我只是删除队列,只是简单地调用json的数据,然后重新加载表。还有一个,我只有当我从JSON获取数据时才重新加载表。 – Nirmalsinh