Search code examples
iosobjective-cuitableviewuiprogressview

Configure UIProgressView in TableViewCell equal to the UIProgressView in the Controller


I have a TableViewCell that contain a UIProgressView, and I set the progress value into UIProgressView in Controller and then equal them(i.e. self.progressBar=cell.progressBar), but in iOS8&9, in the UI the progress bar stuck in 0, but in iOS7 its works. Hope for a help. Thanks~ Below is my code:

@property (strong, nonatomic) IBOutlet UIProgressView *progressBar;

- (void)viewDidLoad{
    [super viewDidLoad];
    self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];
    [self.tableView reloadData];
}

- (void) handleProgressBar{
   if(self.usedTime >= 300.0)
   {
      [self.timer invalidate];
      self.timer=nil;
   }
   else
   {
      self.usedTime += 1;
      CGFloat progress = self.usedTime*(0.0033333333);
      [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
      if(self.usedTime>200){
         [self.progressBar setProgressTintColor:[UIColor redColor]];} 
   }
}

- (void)updateProgress:(NSNumber *)progress {
   float fprogress = [progress floatValue];
   [self.progressBar setProgress:fprogress animated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  switch (indexPath.section) {
    case 0:
        return [self configQuestionCellWithQIndex:self.pageIndex+1];
        break;
    default:
        return nil;
        break;
  }
}

- (QuestionTableViewCell*) configQuestionCellWithQIndex:(NSInteger)qIndex{
QuestionTableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"QuestionCell"];
[cell configCellWithQuestion:self.currentQuestion withQIndex:qIndex];
self.progressBar = cell.progressBar;
return cell;
}

Solution

  • Perform your UI Update in main thread

     dispatch_async(dispatch_get_main_queue(), ^{
    
           cell.progressBar = self.progressBar;
    
            //or 
    
            self.progressBar = cell.progressBar;
    
    
    
    });
    

    Try to perform UI Update on main thread.

    Update :

    you can do something like this to update progress bar in cell,

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
    //... your code...
    cell.progressView.progress = progressValues[indexPath.row];
    // ...
    return cell;
     }
    

    and call this like,

     dispatch_async(dispatch_get_main_queue(), ^{
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
     OFPTableCell *cell = (OFPTableCell*)[self tableView:self.tableViewCache    cellForRowAtIndexPath:indexPath];
     progressValues[indexPath.row] = (double)totalBytesWritten /  (double)totalBytesExpectedToWrite;
     [self.tableViewCache reloadData];
     });
    

    Refer this link for more details.

    hope this will help :)