ios - 滚动表上的 ProgressView block

标签 ios uitableview uiprogressview

我有一个问题。我有一个 UITableView 和一个带有 UIProgressView 的 View ,当我滚动表格时,progressview 不会刷新进度值......只有当滚动完成时,进度才会刷新..

enter image description here

我不知道为什么会这样。
我试图用 dispatch_async 刷新进度

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

//I don't know what i have to put here, maybe the NSTimer??

    dispatch_async(dispatch_get_main_queue(), ^(void){

        //here i set the value of the progress
    });

});

但没有任何改变......

最佳答案

您快到了!

我已经复制了你的问题并修复了它。

这是没有修复的问题,这是我认为您遇到的问题(请注意,滚动时进度指示器不会更新):

enter image description here

这就是修复:

enter image description here

问题是滚动也发生在主线程上并阻塞它。要解决此问题,您只需对计时器进行小幅调整。初始化计时器后,添加以下内容:

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

这是一些示例最小代码:
-(instancetype)init{
    self = [super init];
    if (self) {
        _progressSoFar = 0.0;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.progressIndicator.progress = 0.0;
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(callAfterSomeTime:) userInfo: nil repeats: YES];
    [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];
    [self.myTimer fire];
}

-(void)callAfterSomeTime:(NSTimer *)timer {
    if (self.progressSoFar == 1.0) {
        [self.myTimer invalidate];
        return;
    }

    // Do anything intensive on a background thread (probably not needed)
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        // Update the progress on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            self.progressSoFar += 0.01;
            self.progressIndicator.progress = self.progressSoFar;
        });
    });
}

滚动发生在 UITrackingRunLoopMode .您需要确保您的计时器也处于该模式。除非您进行一些花哨的计算,否则您不应该需要任何后台线程的东西,但我已将其包括在内以防万一。只需在 global_queue 中做任何密集的事情调用但在主线程调用之前。

关于ios - 滚动表上的 ProgressView block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805215/

相关文章:

ios - 在 UITableViewCell 中有条件地移动和删除布局对象

ios - 如何随着时间的推移永久保存文本行

ios - "deselectRow"没有 't animate if using "popViewController”

ios - 在 SwiftUI 中更改 ProgressView 的大小(高度)

ios - Swift 3、iOS 10.3 - 在启动屏幕期间预加载 UIWebView

ios - Swift - 为 CGrect 添加颜色

objective-c - 滚动时表格单元格内容损坏

ios - 来自 url 的 UIImage 需要很长时间才能快速加载

ios - 如果 UIProgressView.progress 正在移动

ios - 如何在 UITextField 底部显示加载进度条