将 UIProgressBar 添加到 UITableViewCell

Add UIProgressBar to UITableViewCell

我需要在流式传输音频剪辑时在 UITableviewCell 中显示 UIProgressBar

我尝试了 运行 和 NSTimer 然后尝试更新进度视图,但它不起作用。这是我的代码。请让我知道这种方法有什么问题。

运行 定时器

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

更新 TableviewCell 中的 UIProgressView

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

输出应该与此类似

当您调用 [self.tblView reloadData] 时,您将在下一行 fCell.progressPlay.progress = 0.0 中将进度条设置回 0.0。删除对 reloadData.

的调用

终于在将近 2 天后发现问题 :( :( 错误在这一行

错误

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

更正

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