2010-09-29 67 views
3

我有一個UIProgressView作爲子視圖添加到self.view。我有一個加載行的UITableView。 我需要在每一行的圖像,但我不希望應用程序等待所有的人,所以我決定運行NSThread加載圖像的過程。當我嘗試從我的線程中更新UIProgressView的進度時 - 它不會更新。 我是否需要實施一些代表?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;............. 

和什麼都沒有....

+0

你確定公關是你期望它? – Daniel 2010-09-29 14:57:57

+0

'updateProgressBar:'看起來像什麼?我不建議更改progressView.progress除主線程以外的任何地方。 – 2010-09-29 18:19:44

+0

您必須更新主線程上的UI,而不是輔助線程上的UI。 – Nyx0uf 2010-09-29 18:25:20

回答

0

我與iOS有同樣的問題5.0。似乎直到5.0,你可以從任何線程更新'progress'變量,但必須從主線程調用setNeedsDisplay。從5.0開始,你必須在主線程中設置'progress'值。希望它可以幫助任何人。