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 の代わりに何か他のものを使用する必要がありますか?