0

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

提前致谢

康斯坦丁

4

2 回答 2

2

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

(void)connectionDidFinishLoading:(NSURLConnection *)aConnection

完成处理后的方法。

于 2013-09-27T09:30:35.800 回答
0

您好,我用一个公共 url 修改了代码,它的工作正常。查看对解决您的问题有用的代码:

- (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;
}
于 2013-09-27T09:43:25.640 回答