2

サーバーからセルのデータを取得するときに、テーブルビューの表示に問題があります。写真を使わないとスクロールが途切れませんが、画像も使いたいです。どうすればこれを解決できますか?サーバーの plist からデータを取得しています。

スクロールを中断させる画像のコードは次のとおりです(カスタムスタイルセルを使用しています)

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


{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];




    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }


    NSURL *URL = [[NSURL alloc] initWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageName"]];

    NSData *URLData = [[NSData alloc] initWithContentsOfURL:URL];



    UIImage *img = [[UIImage alloc] initWithData:URLData];

    UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];

    imgView.image = img;

....
4

1 に答える 1

2

スクロールが停止して開始することを意味する場合、これは、画像がサーバーから読み込まれる場合 (これにはかなりの時間がかかる場合があります)、メイン スレッドで実行するとフリーズが発生するためである可能性があります。

この場合、修正は別のスレッドで画像をフェッチすることです。幸いなことに、iOS には Grand Central Dispatch と呼ばれる非常に使いやすいマルチスレッド システムがあります。

コード例を次に示します。

    dispatch_queue_t q = dispatch_queue_create("FetchImage", NULL);
    dispatch_async(q, ^{
        /* Fetch the image from the server... */
        dispatch_async(dispatch_get_main_queue(), ^{
            /* This is the main thread again, where we set the tableView's image to
               be what we just fetched. */
        });
    });
于 2013-03-28T00:02:19.193 に答える