0

アップルのサンプルコード「LazyTableImages」からのこのスニペットがあります。以下のコードでは、IconDownloaderクラスを初期化しています。それで、これはどのような振る舞いですか。

*************************This Line ******************************************
    IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath]; 

**************************************************************************

その後

    if (iconDownloader == nil) 
    {
        iconDownloader = [[IconDownloader alloc] init];
        iconDownloader.CustomObject = CustomObject;
        iconDownloader.indexPathInTableView = indexPath;
        iconDownloader.delegate = self;
        [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
        [iconDownloader startDownload];
        [iconDownloader release];   
    }

そしてobjectForKeyドキュメントはこれを言います:

objectForKey:

指定されたキーに関連付けられた値を返します。

- (id)objectForKey:(id)aKey
Parameters

aKey

    The key for which to return the corresponding value.

Return Value

The value associated with aKey, or nil if no value is associated with aKey.
Availability

    * Available in iPhone OS 2.0 and later.

だから私は彼らがこの線を設定していると信じるべきです

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

オブジェクトにnil値を設定するためだけに。

最終的に問題は、上記の行は何をするのかということです。

ありがとう

4

2 に答える 2

3

この行:

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

新しい iconDonwloader を作成していません。imageDownloadsInProgress オブジェクト (これは NSDictionary だと思いますか?) に、キー 'indexPath' (テーブルの現在の行) に対応する IconDownloader オブジェクトを取得しようとするだけです。

このコードのビット:

if (iconDownloader == nil) 
{
    iconDownloader = [[IconDownloader alloc] init];
    iconDownloader.CustomObject = CustomObject;
    iconDownloader.indexPathInTableView = indexPath;
    iconDownloader.delegate = self;
    [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
    [iconDownloader startDownload];
    [iconDownloader release];   
}

存在するかどうかを確認します。そうでない場合 (imageDownloadsInProgress が nil を返した、つまり、そのキーのオブジェクトが見つからない場合)、新しいものを作成し、それを imageDownloadsInProgress NSDictionary に追加します。

このコードはすべて、各 indexPath (テーブルの各行) に対して IconDownloader オブジェクトが 1 つしか存在しないことを意味します。これにより、テーブルを上下にスクロールするときにアイコンを複数回ダウンロードしようとする必要がなくなりました。

それが役立つことを願っています。

于 2010-07-12T11:19:27.953 に答える
1

imageDownloadsInProgress は NSMutableDictionary のようです。このディクショナリは、IconDownloader クラスのインスタンスを保持するために使用されていました。インスタンスは対応する indexPath の下に格納されるため、tableView の特定の行の IconDownloader を簡単に取得できます。

あなたが尋ねる行はこれを行います。IconDownloader が以前にインスタンス化されておらず、ディクショナリに格納されていない場合は、指定された indexPath または nil の IconDownloader インスタンスを取得します。

于 2010-07-12T11:20:54.477 に答える