[英]UIProgressView not appearing on UITableViewCell
我在iOS故事板上有一个原型单元,其中包含UIProgressView
。
定期执行的后台进程会通知委托它已经开始。 该委托应使UIProgressView
在表单元格上可见,但这没有发生。 即使我可以看到该委托被调用,也不会导致UIProgressView
出现。
委托方法试图像这样获取指向UIProgressView
的指针:
UIProgressView* view = (UIProgressView*) [[[self tableView:myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] contentView] viewWithTag:MyProgressViewTag];
凡viewWithTag
设置为的标签UIProgressView
。
我尝试调用[myTableView reloadData]
和[myTableView setNeedsDisplay]
来尝试强制重绘单元格,但是它没有用。
有任何想法吗?
您从tableView的数据源请求一个新的单元格,则您获得的单元格不是tableView的一部分。
您想要一个已经在tableview中的单元格,因此向tableView询问该单元格。
尝试这个:
UIProgressView* view = (UIProgressView*) [[[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] contentView] viewWithTag:MyProgressViewTag];
并确保您从mainThread调用它。 您不能通过不是主线程的线程来操作UI对象。
尝试:
[myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil];
所有UI操作必须在主线程上执行。
希望能帮助到你。
只是一个猜测,但是如果您的后台进程在除主线程之外的其他线程上运行,则不会更新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];
});
}
}
祝好运。
蒂姆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.