0

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;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Winston
  • 49
  • 8
  • Can you add code of QuestionTableViewCell where you create progressBar? Why you use IBOutlet for progressBar property? Also try to add a calling updateProgress: into configQuestionCellWithQIndex: method. – Andrew Romanov Apr 28 '16 at 04:22
  • Here is the QuestionTableViewCell.h: #import .@class Question; .@interface QuestionTableViewCell : UITableViewCell .@property (strong, nonatomic) IBOutlet UILabel *qIndex; .@property (strong, nonatomic) IBOutlet UILabel *title; .@property (strong, nonatomic) IBOutlet UIProgressView *progressBar; - (void) configCellWithQuestion:(Question*)q withQIndex:(NSInteger)qIndex; .@end – Winston Apr 28 '16 at 05:34

2 Answers2

0

Just a quick thought - have you tried swapping the line:

self.progressBar = cell.progressBar;

With:

cell.progressBar = self.progressBar;

If that fixes it, then I can't see how it could work on iOS7, but not iOS8 or iOS9 though.

Another option would be to use different instances of UIProgressView in the ViewController and the UITableViewCell - and perhaps updating the UIProgressView in the cell using notifications from your updateProgress: method?

siburb
  • 4,880
  • 1
  • 25
  • 34
  • Hi, Siburb, When I change to cell.progressBar=self.progressBar, iOS7 will also not working. Any idea? Yes i use [self.progressBar setProgress:fprogress animated:YES]; to set the progress into self.progressBar, but now i think i can update the progressbar in the cell, so it stuck in UI, any idea? – Winston Apr 28 '16 at 05:12
0

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 :)

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • Hi, Lion, Thanks for your help!! But can you explain me more, because i am a beginner for this, thanks again~ – Winston Apr 28 '16 at 06:10
  • Also, i have a function doing in viewDidLoad actually, and use a timer for repeating the handleProgressBar function, so how to configure it in cellForRowAtIndexPath? – Winston Apr 28 '16 at 06:12
  • refer the link i have given in answer – Ketan Parmar Apr 28 '16 at 06:21