【问题标题】:Constantly sending data from tableview to tableview cell class不断地将数据从 tableview 发送到 tableview 单元格类
【发布时间】:2014-09-27 13:12:27
【问题描述】:

我有一个NSObject 类,它有一个AVAudioPlayer 和一个NSTimer,我在包含我的tableview 的视图控制器中使用它们。当我点击一个单元格时,AVAudioPlayer 用于播放与该单元格关联的音频文件,并启动 NSTimer。我要做的是将我的进度变量(浮点数)的值发送回我的自定义表格视图单元格以更新进度条,该值等于音频播放器的 currentTime 除以音频文件的持续时间。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Recording * recording = [self.fetchCon objectAtIndexPath:indexPath];
    NSURL * audioURL = [[NSURL alloc] initWithString:recording.audioURL];
    self.audioPlayer = [[OSTablePlayerController alloc] init];
    NSData *data=[NSData dataWithContentsOfURL:audioURL];
    [self.audioPlayer playAudio:data];

    self.audioPlayer.timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.audioPlayer.timer forMode:NSRunLoopCommonModes];
}

 - (void)updateProgress:(NSTimer *)timer 
{
    NSTimeInterval playTime = [self.audioPlayer currentTime];
    NSTimeInterval duration = [self.audioPlayer duration];

    float progress =  playTime/duration;

    //send progress variable to tableview cell class

    if(!(self.audioPlayer.player.playing))
    {
        [self audioPlayerDidFinishPlaying:self.audioPlayer.player successfully:YES];

        progress = 0.00;

        //send progress variable to tableview cell class

    }
}

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if(flag)
    {
        [self.audioPlayer.timer invalidate];
        self.audioPlayer.isPlayerPlaying = NO;
    }
}

【问题讨论】:

    标签: ios uitableview avfoundation nstimer avaudioplayer


    【解决方案1】:

    你可以像这样抓住细胞

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];
       /* further code to set up the timer etc...*/
    }
    

    另一种方法是缓存当前选定的单元格并更新单元格上的进度值。

    不过,我建议您创建UITableViewCell 的子类。然后,在子类中,您有一个播放音频内容并启动计时器的方法。单元本身现在将观察计时器的进度。请注意,如果您继承UITableViewCell,您可能想要实现prepareForReuse

    【讨论】:

    • 嘿 Lev,我目前有一个自定义的 UITableViewCell 类,它控制播放/暂停功能并包含 NSTimer。我认为最好将计时器放在带有 tableview 的实际视图控制器中,以使我的 tableview 单元尽可能地哑(有人告诉我这是正确的做法)。使用我当前的设置,当我点击一个单元格时,进度条将开始更新。但是,如果我在播放单元格时滚动,重复使用的单元格的进度条也会更新......
    • 我认为您需要在 prepareForReuse 方法中创建一个新的进度条。就我个人而言,我不喜欢让单元格保持沉默,因为我发现它经常使表格或集合视图控制器的责任超载。但是,我确实认为没有适当的方法可以做到这一点。您应该对什么是让您的应用执行您想要的操作的最简单方法做出一些判断。
    • 听起来不错。我知道当我向下滚动时,我可以通过使计时器无效并将prepareForReuse中的进度设置为零来清除重用单元格的进度条。但是,如果我向上滚动到选定的单元格,如何重新启动计时器并将其进度设置到应有的位置?
    • 您可以实现 UITableViewDelegate 方法 tableView:willDisplayCell:forRowAtIndexPath: 并检查 indexPath 是否等于当前选择的 indexPath(您可能需要包含一个保存所选 indexPath 的实例变量)。如果它是正确的 indexPath 则再次启动您的 NSTimer 以开始更新单元格的进度值。
    猜你喜欢
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2021-10-16
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多