1

我的代码有点问题,我希望在这里得到一些帮助

我有一个 uitableview,在每个 uitableviewcell 中我放了一个单独的 uislider。每个 uislider,用作音乐播放的进度条。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UIButton *button = nil;
    UISlider *customSlider = nil;

    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UIImage *image = [UIImage imageNamed:@"button_play.png"];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(340.0, 10.0, image.size.width, image.size.height);
        button.frame = frame;
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 4;
        [cell.contentView addSubview:button];

        customSlider = [[UISlider alloc] initWithFrame:CGRectMake(10, 45, 456, 20)];    
        customSlider.minimumValue = 0.0;
        customSlider.maximumValue = 100.0;
        customSlider.continuous = YES;
        customSlider.tag = 3;
        customSlider.value = 0.0;
        [cell.contentView addSubview:customSlider];
    }

    else {
        customSlider = (UISlider *)[cell.contentView viewWithTag:3];
        button = (UIButton *)[cell.contentView viewWithTag:4];
    }
    return cell;
}


- (void) playAudio:(UIButton *)sender {


    UIButton *button = (UIButton *)[sender superview];
    UITableViewCell *currentCellTouched = (UITableViewCell *)[button superview];
    UITableView *currentTable = (UITableView *)[currentCellTouched superview];
    NSIndexPath *indexPath = [currentTable indexPathForCell:currentCellTouched];

    //currentCellPlaying type of UITableViewCell and accessible from the rest classe
    currentCellPlaying = currentCellTouched;

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:NSLocalizedString([listFilesAudio objectAtIndex:indexPath.row], @"") ofType:@"mp3"]];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    player.delegate = self;
    [player prepareToPlay];

    [(UISlider *)[currentCellTouched.contentView viewWithTag:3] setMaximumValue:[player duration]];
    [(UISlider *)[currentCellTouched.contentView viewWithTag:3] setValue:0.0];

    NSTimer *sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
    [player play];

}


- (void)updateTime:(NSTimer *)timer {
    [(UISlider *)[currentCellPlaying.contentView viewWithTag:3] setValue:player.currentTime];
}

当我启动一个游戏时,一切都很好,进度条也更新了。

我的问题是,当我向下/向上滚动表格视图时,正在播放音乐的单元格消失了,一旦我们回到播放音乐的单元格,uislider 又回到 0,并且不再更新......(一个 NSLOG 确认我们仍然进入“updateTime”方法)

如果您有我的问题的解决方案,我会很高兴阅读它。

先感谢您。

4

2 回答 2

2

Otium 的答案并不完全正确,但它朝着正确的方向发展。单元格并非都是同一个对象,但是(实际上您是这样实现的)通过滚动离开可见区域的单元格被重用于显示其他单元格,滚动到可见区域。因此,当播放音乐的单元格再次可见时,其中会显示另一个滑块对象,然后是原始滑块对象。此外,currentCellPlaying对象(可能)不再显示或显示为另一个单元格。因此,当您更新时,viewWithTag:3您(有时)不会看到。您应该做的是存储“indexCurrentPlaying”而不是单元格或滑块引用。使用该索引,您可以在 ... 末尾装饰单元格。cellForRowAt您可以在该索引内部更新该单元格的滑块updateTime

编辑:

一些应该可以工作的未经测试的代码(也许有一些修复,但我认为它解释了这个想法):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UIButton *button = nil;
    UISlider *customSlider = nil;

    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UIImage *image = [UIImage imageNamed:@"button_play.png"];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(340.0, 10.0, image.size.width, image.size.height);
        button.frame = frame;
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 4;
        [cell.contentView addSubview:button];

        customSlider = [[UISlider alloc] initWithFrame:CGRectMake(10, 45, 456, 20)];    
        customSlider.minimumValue = 0.0;
        customSlider.maximumValue = 100.0;
        customSlider.continuous = YES;
        customSlider.tag = 3;
        customSlider.value = 0.0;
        [cell.contentView addSubview:customSlider];
    }
    else
    {
       customSlider = [cell viewWithTag:3];     
    }
    if([indexPath isEqual:currentPlayingIndexPath])
    {
       currentPlayingSlider = customSlider; 
       [self updateTime];
    } 
    else if(customSlider == currentPlayingSlider)
    {
       currentPlayingSlider = nil;  
    }
    return cell;
}


- (void) playAudio
{

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:NSLocalizedString([listFilesAudio objectAtIndex:currentPlayingIndexPath.row], @"") ofType:@"mp3"]];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    player.delegate = self;
    [player prepareToPlay];

    NSTimer *sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
    [player play];
}


- (void)updateTime:(NSTimer *)timer 
{
    [currentPlayingSlider setValue:player.currentTime];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   currentPlayingIndexPath = indexPath;
   currentPlayingSlider = [cellForRowAtIndexPath: currentPlayingIndexPath]; 
   [self playAudio];    
}

小心NSIndexPath isEqual。似乎在不同版本的 SDK 中回答不同(看这里)......因为您只需要可以将其更改为的行currentPlayingIndexPath.row == indexPath.row

于 2012-04-04T22:12:59.720 回答
0

UItableView 使用可取消队列的单元格,因此您的所有单元格基本上都是同一个对象。在 cellForRowAtIndexPath 方法中,您应该再次设置滑块的进度。

于 2012-04-04T21:27:20.377 回答