0

我在 iOS 故事板上有一个原型单元格,其中包括UIProgressView.

定期执行的后台进程会通知委托人它已启动。该委托应该使UIProgressView表格单元格上可见,但这没有发生。即使我可以看到被调用的委托,它也不会导致UIProgressView出现。

委托方法尝试获取指向UIProgressView这样的指针:

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

whereviewWithTag设置为UIProgressView.

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

有任何想法吗?

4

3 回答 3

3

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

你想要一个已经在 tableview 中的单元格,所以向 tableView 询问那个单元格。

尝试这个:

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

并确保从 mainThread 调用它。您不能从不是主线程的线程操作 UI 对象。

于 2012-02-23T17:59:02.683 回答
1

尝试:

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

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

希望能帮助到你。

于 2012-02-23T18:03:36.687 回答
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];
                });
            }

        }

祝你好运。

蒂姆

于 2012-02-23T18:05:13.933 回答