2016-03-04 59 views
1

我遇到问题。我有一个UITableView和一个UIProgressView视图,当我滚动表时,progressview不刷新进度值......只有当滚动完成,进度刷新..滚动表上的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 
    }); 

}); 

,但没有改变的进展...

回答

2

你几乎没有!

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

这是它没有修复,这是我觉得你(注意进度指示器不会更新滚动时)这个问题:

enter image description here

,这是它与修复:

enter image description here

的问题是,滚动也发生在主线程和块它。要解决这个问题,你只需要对你的定时器进行一些小调整。你初始化你的计时器后,补充一点:

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

下面是一些例子最少的代码:在UITrackingRunLoopMode发生

-(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; 
     }); 
    }); 
} 

滚动。您需要确保您的计时器也处于该模式。你不应该需要任何后台线程的东西,除非你做一些奇特的计算,但我已经包含它以防万一。只需在global_queue调用中但在主线程调用之前执行任何密集型内容。

+0

YEAH !!非常感谢你!! – Quetool