Повторное использование UITableViewCell с GCD

Я использую Grand Central Dispatch для асинхронной загрузки изображений UITableViewCell. Это работает хорошо, за исключением некоторых пограничных случаев, когда ячейка используется повторно, а предыдущий блок загружает неправильное изображение.

Мой текущий код выглядит так:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *imagePath = [self imagePathForIndexPath:indexPath];         
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

        dispatch_sync(dispatch_get_main_queue(), ^{
            cell.imageView.image = image;
            [cell setNeedsLayout];
        });
    });

    return cell;
}

Насколько я знаю, очереди GCD нельзя остановить. Как тогда можно предотвратить этот пограничный случай? Или я должен использовать что-то другое вместо GCD для решения этой проблемы?

5
задан hpique 15 August 2012 в 07:11
поделиться