如何更新此线程中间的UI?

law*_*nce 3 iphone cocoa-touch objective-c

下面是一个代码块,它在我应用程序主线程的一个单独的线程中运行.如何在每个按钮获取缩略图后更新UI?现在它不会更新,直到整个方法完成.这些按钮已添加到UIScrollView中.

(LotsGridButton只是一个带有一些额外属性的UIButton.)

- (void)fetchThumbnails {
    CCServer* server = [[CCServer alloc] init];
    for (int i=0; i<[buttons count]; i++) {
        LotsGridButton* button = [buttons objectAtIndex:i];     
        if (button.lot.thumbnail) continue;
        // load the thumbnail image from the server
        button.lot.thumbnail = [server imageWithPath:button.lot.thumbnailURL];
        [button setImage:button.lot.thumbnail forState:UIControlStateNormal];
    }
    [server release];
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*lds 5

代替setImage:forState:,看看performSelectorOnMainThread:方法,例如:

[myButton performSelectorOnMainThread:@selector(setThumbnail:) withObject:[server imageWithPath:myButton.lot.thumbnailURL] waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)