UITableView重新加载数据需要永远

时间:2013-09-27 09:20:32

标签: objective-c uitableview reloaddata

我通过JSON将一些东西解析成表视图,但我遇到的问题是解析需要大约一秒钟(我知道这是由于网络指示器)但是然后有一个巨大的延迟,直到数据出现在表格视图。

我试着放置[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 个答案:

答案 0 :(得分:2)

您正在主线程上加载同步的数据,这是坏的并阻塞了接口。 尝试使用NSURLConnection加载JSON数据并在

中重新加载UITableView
(void)connectionDidFinishLoading:(NSURLConnection *)aConnection

您完成处理后的方法。

答案 1 :(得分: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;
}