Iphone UIProgressView进度不显示';t更新
我在self.view中添加了一个UIProgressView作为子视图。我有一个加载了行的UITableView。 我需要每一行都有图像,但我不希望应用程序等待所有图像,所以我决定运行NSThread并加载图像进程。当我试图从线程内部更新UIProgressView的进度时,它不会得到更新。 我是否需要实施一些委托 初始化Iphone UIProgressView进度不显示';t更新,iphone,cocoa-touch,nsthread,uiprogressview,Iphone,Cocoa Touch,Nsthread,Uiprogressview,我在self.view中添加了一个UIProgressView作为子视图。我有一个加载了行的UITableView。 我需要每一行都有图像,但我不希望应用程序等待所有图像,所以我决定运行NSThread并加载图像进程。当我试图从线程内部更新UIProgressView的进度时,它不会得到更新。 我是否需要实施一些委托 初始化 progressView = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
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;.............
cgpr;
对于(int i=0;i<[self.itemsToDisplay count];i++){
如果(!runThread){
返回;
}
pr=(CGFloat)i/(CGFloat)[self.itemsToDisplay count];
[self-performSelectorOnMainThread:@selector(updateProgressBar:)
withObject:[NSNumber numberWithFloat:pr]
waitUntilDone:是的;
progressView.progress+=0.5;。。。。。。。。。。。。。
什么都没有……我在iOS 5.0上也遇到了同样的问题。似乎在5.0之前,您可以从任何线程更新“进度”变量,但必须从主线程调用setNeedsDisplay。从5.0及以后,您还必须从主线程设置“进度”值。希望这对任何人都有帮助。您所要做的就是,更新主线程上的
Progressview
。下面是关于它的文章。
你确定pr是你所期望的吗?
updateProgressBar:
看起来像什么?我不建议更改progressView.progress,除了主线程。你必须更新主线程上的UI,而不是次线程上的UI。他使用的是performSelectorOnMainThread:
,所以线程问题已经解决了。updateProgressBar looks类似-(void)updateProgressBar:(NSNumber*)num{[progressView setProgress:[num floatValue]];}