テーブルビューにサムネイルの行を非同期でロードしようとする次のコードがあります。
for (int i = 0; i < totalThumbnails; i++)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
__block GraphicView *graphicView;
__block Graphic *graphic;
dispatch_async(dispatch_get_main_queue(),
^{
graphicView = [[tableViewCell.contentView.subviews objectAtIndex:i] retain];
graphic = [[self.thumbnailCache objectForKey: [NSNumber numberWithInt:startingThumbnailIndex + i]] retain];
if (!graphic)
{
graphic = [[self graphicWithType:startingThumbnailIndex + i] retain];
[self.thumbnailCache setObject: graphic forKey:[NSNumber numberWithInt:startingThumbnailIndex + i]];
}
[graphicView setGraphic:graphic maximumDimension:self.cellDimension];
});
[graphicView setNeedsDisplay];
dispatch_async(dispatch_get_main_queue(),
^{
CGRect graphicViewFrame = graphicView.frame;
graphicViewFrame.origin.x = ((self.cellDimension - graphicViewFrame.size.width) / 2) + (i * self.cellDimension);
graphicViewFrame.origin.y = (self.cellDimension - graphicViewFrame.size.height) / 2;
graphicView.frame = graphicViewFrame;
});
[graphicView release];
[graphic release];
});
}
ただし、コードを実行すると、次の行にアクセスできなくなります。次[graphicView setNeedsDisplay];
のように設定すると、コードが正常に動作することに注意してください。
for (int i = 0; i < totalThumbnails; i++)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
dispatch_async(dispatch_get_main_queue(),
^{
//put all the code here
});
}
それは正常に動作し、UITableView
最初に呼び出されたときに非同期にロードされますが、スクロールは依然として非常に途切れ途切れです。
したがって、メイン スレッドではなくグローバル スレッドで描画を実行できるように、コードの最初のビットを機能させるために を取得したいと思います (途切れ途切れのスクロールが修正されると思いますか?)。
iOS4 の描画は非同期で実行できるので、それが問題だとは思いません。おそらく私は__Block
タイプを誤用していますか?
これを機能させる方法を知っている人はいますか?