3

我有一个 UIProgressView 添加到 self.view 作为子视图。我有一个加载了行的 UITableView。我需要每一行中的图像,但我不希望应用程序等待所有图像,所以我决定运行 NSThread 并加载图像过程。当我尝试从线程内部更新 UIProgressView 的进度时 - 它没有得到更新。我可能需要实施一些代表吗?

初始化

    progressView = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
    [progressView setFrame:CGRectOffset(CGRectMake(0, 0, 320, 8), 0, 1)];
    [self.view addSubview:progressView];

然后我运行我的线程

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [progressView setProgress:0.0];
    NSThread *myThread = [[NSThread alloc] initWithTarget:self
                                                 selector:@selector(imageLazyLoading)
                                                   object:nil];

    [myThread start];
    [pool release];
    [myThread release];

然后我尝试更新它

CGFloat pr;
for(int i=0; i < [self.itemsToDisplay count]; i++){
    if (!runThread) {
        return;
    }
    pr = (CGFloat)i/(CGFloat)[self.itemsToDisplay count]; 
    [self performSelectorOnMainThread:@selector(updateProgressBar:)
                           withObject: [NSNumber numberWithFloat:pr]
                        waitUntilDone: YES];
    progressView.progress += 0.5;.............

什么都没有……

4

2 回答 2

0

您所要做的就是更新Progressview主线程。这是关于它的文章。 http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/

于 2012-10-25T07:31:21.143 回答
0

我在 iOS 5.0 上遇到了同样的问题。似乎直到 5.0,您可以从任何线程更新“进度”变量,但必须从主线程调用 setNeedsDisplay。从 5.0 起,您还必须从主线程设置“进度”值。希望它可以帮助任何人。

于 2011-11-30T12:03:03.490 回答