smartsanja
smartsanja

Reputation: 4550

Add UIProgressBar to UITableViewCell

I have a requirement to display a UIProgressBar in UITableviewCell while streaming an audio clip.

I tried running a NSTimer and then trying to update the progress view, but its not working. This is my code. Please let me know whats wrong with this approach.

Run the timer

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25
                                          target:self
                                        selector:@selector(updatePlayProgress)
                                        userInfo:nil
                                         repeats:YES];

Update UIProgressView in TableviewCell

- (void)updatePlayProgress {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSLog(@"Duration %f", appDelegate.moviePlayer.duration);
    NSLog(@"Current time %f", appDelegate.moviePlayer.currentPlaybackTime);

    float timeLeft = appDelegate.moviePlayer.currentPlaybackTime/appDelegate.moviePlayer.duration;

    // upate the UIProgress
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:playingIndex inSection:0];
    FeedCell *cell = (FeedCell*)[self tableView:self.tblView cellForRowAtIndexPath:indexPath];

    NSLog(@"Time left %f", timeLeft);

    cell.progressPlay.progress = 0.5;

    [cell.progressPlay setNeedsDisplay];
}

CellForRowAtIndexPath

fCell.progressPlay.alpha = 0.5;
fCell.progressPlay.tintColor = navigationBarColor;
[fCell.progressPlay setTransform:CGAffineTransformMakeScale(fCell.frame.size.width, fCell.frame.size.height)];

[fCell.progressPlay setHidden:NO];

return fCell;

Output should be something similar to this

enter image description here

Upvotes: 11

Views: 387

Answers (2)

smartsanja
smartsanja

Reputation: 4550

Eventually found the issue after almost 2 days :( :( Error was in this line

Wrong

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:playingIndex inSection:0];
FeedCell *cell = (FeedCell*)[self tableView:self.tblView cellForRowAtIndexPath:indexPath];

Corrected

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:playingIndex inSection:0];
FeedCell *cell = (FeedCell*)[self.tblView cellForRowAtIndexPath:indexPath]; 

Upvotes: 1

Michael
Michael

Reputation: 6907

When you call [self.tblView reloadData] you are setting the progress bar back to 0.0 in the following line fCell.progressPlay.progress = 0.0. Remove the call to reloadData.

Upvotes: 0

Related Questions