如何在这个线程的中间更新UI? [英] How do I update the UI in the middle of this thread?

查看:109
本文介绍了如何在这个线程的中间更新UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is a block of code that runs in a separate thread from my app's main thread. How do I get the UI to update after each button gets its thumbnail? Right now it doesn't update until the whole method finishes. The buttons are already added to a UIScrollView.

(LotsGridButton只是一个具有一些额外属性的UIButton。)

(LotsGridButton is just a UIButton with some extra properties.)

- (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];
}


推荐答案

$ c> setImage:forState:,请查看 performSelectorOnMainThread:方法,例如:

In place of setImage:forState:, take a look at the performSelectorOnMainThread: method, e.g.:

[myButton performSelectorOnMainThread:@selector(setThumbnail:) withObject:[server imageWithPath:myButton.lot.thumbnailURL] waitUntilDone:NO];

这篇关于如何在这个线程的中间更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆