2012-04-04 7 views
1

をスクロールするとき、私はそれぞれに、のUITableViewを持っているが、私は入れていないのUITableViewCellです個々のuislider。 各uisliderは、音楽再生の進行状況バーとして使用されます。Uisliderは何より、自動更新、私は私のコードで少し問題を抱えている、と私はここに</p> <p>をいくつかの助けを得るために期待していた

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

1回の再生を開始すると、すべてが正常で進行状況バーが更新されます。

私の問題は、テーブルビューを下にスクロールすると、音楽が再生されているセルが消えると、音楽を再生しているセルに戻ると、uisliderは0に戻り、もう更新されません...(NSLOGはまだ "updateTime"メソッドの中に入っていることを確認しています)

私の問題の解決策があれば、私はそれを読むことをとてもうれしく思います。

ありがとうございます。

答えて

0

UItableViewはデキュー可能なセルを使用するため、すべてのセルは基本的に同じオブジェクトです。 cellForRowAtIndexPathメソッドでは、スライダの進行状況を再度設定する必要があります。

2

オーティウムの答えは完全ではありませんが、正しい方向に向いています。セルはすべて同じオブジェクトではありませんが、実際にはその方法で実装しています。スクロールで可視領域を残しているセルは、他のセルの表示に再利用され、可視領域にスクロールします。したがって、音楽を再生しているセルが再度表示されると、もう1つのスライダオブジェクトが表示され、その中に元のものが表示されます。さらに、currentCellPlayingオブジェクト(多分)はもう表示されないか、別のセルとして表示されます。したがって、あなたがそれを更新すると、viewWithTag:3(時々)それを見ることはありません。 あなたがすべきことは、セルまたはスライダーの代わりに "indexCurrentPlaying"を保存することです。そのインデックスでは、cellForRowAtの最後にセルを飾ることができます。そして、そのインデックスのセルのスライダをupdateTimeの中に更新することができます。 希望に役立ちます。

EDIT:(それは私が考えて、考えを説明するかもしれないいくつかの修正ではなく)

動作するはずのいくつか、テストしていないコード:

- (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(look here)に異なる回答があるようです。行が必要なのはcurrentPlayingIndexPath.row == indexPath.row

+0

です。申し訳ありません。 – Otium

関連する問題