0

JSONを介してテーブルビューにいくつかのものを解析しますが、解析に約1秒かかるという問題があります(ネットワークインジケーターのためにこれを知っています)が、データがテーブルビューに表示されるまでに大きな遅延があります。

[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

メインスレッドにデータ同期をロードしていますが、これは不良であり、インターフェイスをブロックしています。JSON データを NSURLConnection でロードしてみて、UITableView を

(void)connectionDidFinishLoading:(NSURLConnection *)aConnection

処理が終わったらメソッド。

于 2013-09-27T09:30:35.800 に答える
0

こんにちは、1 つの公開 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 に答える