2012-02-23 52 views
0

我在iOS故事板上有一个原型单元格,其中包含UIProgressViewUIProgressView没有出现在UITableViewCell

定期执行的后台进程会通知代理它已启动。此代表应使UIProgressView在表格单元格中可见,但这不会发生。即使我可以看到被调用的代理,它也不会导致UIProgressView出现。

委托方法试图让一个指针UIProgressView这样的:

UIProgressView* view = (UIProgressView*) [[[self tableView:myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] contentView] viewWithTag:MyProgressViewTag]; 

viewWithTag设置为UIProgressView的标签。

我曾尝试致电[myTableView reloadData][myTableView setNeedsDisplay]尝试强制重绘单元格,但它没有奏效。

任何想法?

+0

所有背景中的UI操作都应在主线程上执行。 – NeverBe 2012-02-23 17:59:03

+0

感谢所有回复。它给了我很多东西来看看。然而,一位同事刚刚指出,UITableView之外的另一个UIProgressView(它只是位于视图的顶部)也正在被后台进程更新,但是它正在被更新。我会在早上检查我的代码。 – 2012-02-23 19:56:20

回答

3

你从tableView的数据源请求一个新的单元格,你得到的单元格不是tableView的一部分。

你想要一个已经在tableview中的单元格,所以要求tableView提供该单元格。

试试这个:

UIProgressView* view = (UIProgressView*) [[[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] contentView] viewWithTag:MyProgressViewTag]; 

并确保您从mainThread调用此。您不能从不是主线程的线程处理UI对象。

1

尝试:

[myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil]; 

所有UI操作必须在主线程中执行。

希望它有帮助。

1

只是一个猜测,但如果你的后台进程运行在主线程以外的其他地方,UI将不会被更新。所有对UIKit的调用都需要在主线程中完成。你可以做的是使用Grand Central Dispatch(GCD)并将一个块分派给主队列。即在您需要更新UIProgressView的后台进程中。

dispatch_async(dispatch_get_main_queue(),^{ 
     // your background processes call to the delegate method 
}); 

这个项目展示了如何使用GCD后台进程更新UIProgressView:https://github.com/toolmanGitHub/BDHoverViewController

下面是另一个例子:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0),^{ 
     NSInteger iCntr=0; 
     for (iCntr=0; iCntr<1000000000; iCntr++) { 
      if ((iCntr % 1000)==0) { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [blockSelf.hoverViewController updateHoverViewStatus:[NSString stringWithFormat:@"Value: %f",iCntr/1000000000.0] 
                  progressValue:(float)iCntr/1000000000.0]; 
       }); 
      } 

     } 

好运。

Tim