新的tableview单元格选择控制先前的tableview单元格选择

时间:2014-06-11 04:10:56

标签: ios cocoa-touch

我有一个自定义tableview单元格的tableview,每个单元格都与特定的音频文件相关联。每个单元格里面都有一个进度条。选择行时,我可以播放与单元格关联的音频文件。但是,如果一个单元格正在播放并且我点击另一个单元格,则音频播放器将播放新的音频文件,但先前所选单元格的进度条将与我新选择的单元格的进度条一起移动。我尝试使用deselectRowAtIndexPath但没有运气。

//Selecting row in table view 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSDictionary * packageInArray = [self.audioTableArray objectAtIndex:indexPath.row];
    [((OSAudioTableCell*)[self.audioTable cellForRowAtIndexPath:indexPath]) rowSelected:[packageInArray objectForKey:@"audioData"]];
}

//Method in custom tableview cell 
-(void) rowSelected: (NSData*) data
{
    if(self.audioPlayer.isPlayerPlaying == YES)
    {
        [self.audioPlayer.player stop];
        [self.waveView setProgress:0];
        [self.timer invalidate];
        self.audioPlayer.isPlayerPlaying = NO;
    }

    self.audioPlayer = [OSTablePlayerController getInstance];
    [self.audioPlayer playAudio:data];
    self.audioPlayer.isPlayerPlaying = YES;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:.0001 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];

}

1 个答案:

答案 0 :(得分:0)

您需要拥有以前播放的单元格的indexPath引用。然后,在自定义单元格上实现一种新方法,以停止播放音频。

以下是一些示例代码:

        -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        { 
            NSDictionary * packageInArray = [self.audioTableArray objectAtIndex:indexPath.row];
            [((OSAudioTableCell*)[self.audioTable cellForRowAtIndexPath:indexPath]) rowSelected:[packageInArray objectForKey:@"audioData"]];

            if(previousCellIndexPath != nil) {

                 [((OSAudioTableCell*)[self.audioTable cellForRowAtIndexPath:previousCellIndexPath]) rowDeselected];
            }

            previousCellIndexPath = indexPath;
           // this index path will become 'previous' on the next didSelectRow for future use
        }

将rowSelected方法更改为仅为新选择的单元格播放音乐:

//Method in custom tableview cell 
    -(void) rowSelected: (NSData*) data
    {
        self.audioPlayer = [OSTablePlayerController getInstance];
        [self.audioPlayer playAudio:data];
        self.audioPlayer.isPlayerPlaying = YES;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:.0001 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];

    }

最后,实现一种新方法来停止以前选择的单元格的音乐:

-(void)rowDeselected
{
            [self.audioPlayer.player stop];
            [self.waveView setProgress:0];
            [self.timer invalidate];
            self.audioPlayer.isPlayerPlaying = NO;
}

这应该解决它。