0

JSONからデータを取得してUITableViewに表示する必要があるアプリケーションを開発しています。データの取得はバックグラウンドで行っています。しかし、それは無限ループに陥っているようです。

どんな助けでも大歓迎です。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath];
queue = dispatch_queue_create("com.events.app", NULL);
dispatch_async(queue, ^{
    [self load];
    //Need to go back to the main thread since this is UI related
    dispatch_async(dispatch_get_main_queue(), ^{
        // store the downloaded image in your model
        Events *evObj = [eventsArray objectAtIndex:[indexPath row]];
        NSLog(@"evObj = %@",evObj);
        cell.textLabel.text = evObj.eName;
        cell.detailTextLabel.text = @"Detailed Text";
        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
                              withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView endUpdates];
    });
});
    // Configure the cell...



return cell;
}
4

3 に答える 3

1

すべての非同期呼び出しを削除するだけです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath];

   if(cell == nil) {
     /// load the cell.
   }


    // store the downloaded image in your model
   Events *evObj = [eventsArray objectAtIndex:[indexPath row]];
   NSLog(@"evObj = %@",evObj);
   cell.textLabel.text = evObj.eName;
   cell.detailTextLabel.text = @"Detailed Text";


   return cell;
}

[self load];をコードの他の部分に移動するだけviewDidLoadです。

于 2013-04-04T12:09:14.587 に答える
0

データ部分と ui 部分を区別します。

いえ

[self load];メソッドを一度だけ、または必要な回数だけ呼び出します。これに最適な場所は、viewDidLoad メソッドなどです。

データをリロードして uitableview に表示するための実装は、次のようになります ( GCD を使用)

dispatch_queue_t queue = dispatch_queue_create("com.events.app", NULL);

dispatch_async(queue, ^{
[self load];

//Need to go back to the main thread since this is UI related
dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableview reloadData];

//don't forget to release your queue
dispatch_release(queue);

});

});

メソッドの実装を- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}rckoenes からコピーすれば完了です;)

しかし、1 つ重要なことを覚えておいてください。最初にスローされたコードは大きなオーバーヘッドを引き起こします。このメソッドが呼び出されるたびにデータをダウンロード、解析、および設定しています。最初は uitableview が読み込まれるときに少なくとも 3 回、その後はスクロールするたびに何度でも呼び出されます。画面に表示できる多くの行/セル

于 2013-04-04T12:21:38.193 に答える