UITableViewCell保存按钮状态

时间:2016-04-16 15:29:55

标签: objective-c uitableview uibutton state

我尝试创建一个简单的播放器,播放来自流的音频。我有一个带有自定义单元格的表视图,它具有播放(暂停)按钮和标签。当我按下播放按钮时,我当前的单元格按钮标题已更改为暂停标题,但其他一些单元格也已更改,我可以在向上或向下滚动时看到。正如我所知,我必须在我的tableview中保存每个单元格的状态,但我不清楚我是如何做到这一点的。有谁能解释我,我怎么能这样做。

这是我的代码:

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

[self tableView:tableView heightForRowAtIndexPath:indexPath];

if (indexPath.row == [self.audiosArray count]) {
    static NSString* identifier = @"Cell";

    UITableViewCell* firstCell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!firstCell) {
        firstCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    firstCell.textLabel.text = @"LOAD MUSIC";
    firstCell.imageView.image = nil;
    return firstCell;

} else {
    AudioCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AudioCustomCell class]) forIndexPath:indexPath];



    VYAudioFile *audioFile = [self.audiosArray objectAtIndex:indexPath.row];
    cell.artistLabel.text = audioFile.audioArtist;
    cell.titleLabel.text = audioFile.audioTitle;

    cell.playButton.tag = indexPath.row;
    [cell.playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.cellsArray addObject:cell];
    return cell;

}

}

播放/暂停方法:

 -(void) playAction:(UIButton *) sender
{
    if (![sender isEqual:nil]){
        self.audioIndex = sender.tag;
    }
    else {
        self.audioIndex = self.audioIndex + 1;
    }
    VYAudioFile *localAudio = [self.audiosArray objectAtIndex:self.audioIndex];

    self.previousCell = self.currentCell;
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    self.currentPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    self.currentCell = [self.tableView cellForRowAtIndexPath:self.currentPath];

    NSLog(@"Sender tag : %d", sender.tag);


    if (!self.audioPlayer)
    {
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
        self.currentPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
        self.currentCell = [self.tableView cellForRowAtIndexPath:self.currentPath];

        NSLog(@"!self.audioPlayer Cell button tag : %d", self.currentCell.playButton.tag);

        self.urlAsset = [AVURLAsset URLAssetWithURL:localAudio.audioURL options:nil];
        self.playerItem = [AVPlayerItem playerItemWithAsset:self.urlAsset];
        self.audioPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
        [self.audioPlayer play];
        [self.currentCell.playButton setTitle:@"Pause" forState:UIControlStateNormal];
        self.previousCell = self.currentCell;
    }
    else
    {
        if (self.audioPlayer.rate == 1.0)
        {
            if ([self.currentCell isEqual:self.previousCell])
            {
                [self.audioPlayer pause];
                [self.currentCell.playButton setTitle:@"Play" forState:UIControlStateNormal];
            }
            else
            {

                [self.audioPlayer pause];
                [self resetPlayingAudio];

                CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
                self.currentPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
                self.currentCell = [self.tableView cellForRowAtIndexPath:self.currentPath];

                 NSLog(@"self.audioPlayer && rate = 1 Cell button tag : %d", self.currentCell.playButton.tag);

                self.urlAsset = [AVURLAsset URLAssetWithURL:localAudio.audioURL options:nil];
                self.playerItem = [AVPlayerItem playerItemWithAsset:self.urlAsset];
                self.audioPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
                [self.audioPlayer play];
                [self.currentCell.playButton setTitle:@"Pause" forState:UIControlStateNormal];
                self.previousCell = self.currentCell;
            }
        }
        else
        {
            if ([self.currentCell isEqual:self.previousCell] ) {
                [self.audioPlayer play];
                [self.currentCell.playButton setTitle:@"Pause" forState:UIControlStateNormal];
            }
            else {
                [self resetPlayingAudio];

                CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
                self.currentPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
                self.currentCell = [self.tableView cellForRowAtIndexPath:self.currentPath];

                NSLog(@"self.audioPlayer && rate = 0 Cell button tag : %d", self.currentCell.playButton.tag);

                self.urlAsset = [AVURLAsset URLAssetWithURL:localAudio.audioURL options:nil];
                self.playerItem = [AVPlayerItem playerItemWithAsset:self.urlAsset];
                self.audioPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
                [self.audioPlayer play];
                [self.currentCell.playButton setTitle:@"Pause" forState:UIControlStateNormal];
                self.previousCell = self.currentCell;
            }
        }
    }


}

重置方法:

    -(void) resetPlayingAudio
{
    if (self.audioPlayer.rate == 0.0)
    {
        [self.previousCell.playButton setTitle:@"Play" forState:UIControlStateNormal];
        [self.currentCell.playButton setTitle:@"Play" forState:UIControlStateNormal];
        self.audioPlayer = nil;
        self.previousCell = nil;
        self.currentCell = nil;
    }
    else
    {
        [self.audioPlayer pause];
        [self.previousCell.playButton setTitle:@"Play" forState:UIControlStateNormal];
        [self.currentCell.playButton setTitle:@"Play" forState:UIControlStateNormal];
        self.audioPlayer = nil;
        self.previousCell = nil;
        self.currentCell = nil;
    }

}

AudioCustomCell类:

h文件:

  #import <UIKit/UIKit.h>

@interface AudioCustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UILabel *artistLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) NSURL *audioURL;
@property (assign, nonatomic) BOOL buttonSelected;

@end

m文件:

    #import "AudioCustomCell.h"

@implementation AudioCustomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.buttonSelected = NO;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

0 个答案:

没有答案