1

私のuitableviewがうまくスムーズにスクロールしないため、スクロール中にuitableviewセルにカスタムオブジェクトを非同期で挿入するための簡単なチュートリアルを見つけようとしています。検索しましたが、画像の非同期読み込みが役に立たないことがわかりました。非同期でロードする必要がある uiview があります。スクロールがスムーズではないため、オブジェクトをロードする前に必要な処理作業が多すぎます。

どんな助けでも感謝します。

4

1 に答える 1

4

これは見た目ほど難しくありません。1 つだけ注意事項があります。セルが完全にロードされていない場合でも、セルの高さを知っておく必要があります。

tableView の行の高さが一定の場合は、tableView.rowHeight を設定します。その場で行の高さを決定する必要がある場合は、UITableViewDelegate の–tableView:heightForRowAtIndexPath:コールバックを使用します。

次に-tableView:cellForRowAtIndexPath、セルをデキューし、初期状態に設定し、NSOperation または GCD ブロックを開始して、最後にリセットしたセルを返します。

NSOperation または CCG ブロックでは、必要な作業を実行してから、メイン スレッドにコールバックして値をセルに設定します。これが非同期セル読み込みの本質です。


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // dequeue a cell

    // Reset the cell
    cell.imageView.image = nil;
    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;

    // Use gcd
    dispatch_queue_t queue = dispatch_queue_create("blah blah replace me blah", 0);
    dispatch_async(queue, ^{ 

        // Do work in the background

        UIImage *image       = value1;
        NSString *text       = value2;
        NSString *detailText = value3;

        dispatch_async(dispatch_get_main_queue(), ^{ 
            // Back to main thread to set cell properties.
            if ([tableView indexPathForCell:cell].row == indexPath.row) {
                cell.imageView.image      = image;
                cell.textLabel.text       = text;
                cell.detailTextLabel.text = detailText;
            }
        });//end
    });//end
    dispatch_release(queue);

    // Return the reset cell
    return cell;
}
于 2012-05-19T01:43:02.800 に答える