Objective c UITableViewCell保存按钮状态
我尝试创建一个简单的播放器,从流播放音频。我有一个带有自定义单元格的表格视图,其中有播放(暂停)按钮和标签。当我按下“播放”按钮时,我当前的单元格按钮标题已更改为“暂停”标题,但其他一些单元格也已更改,我可以在上下滚动时看到。正如我发现的那样,我必须在tableview中保存每个单元格的状态,但我不清楚如何才能做到这一点。谁能解释一下我是怎么做到的 这是我的密码:Objective c UITableViewCell保存按钮状态,objective-c,uitableview,uibutton,state,Objective C,Uitableview,Uibutton,State,我尝试创建一个简单的播放器,从流播放音频。我有一个带有自定义单元格的表格视图,其中有播放(暂停)按钮和标签。当我按下“播放”按钮时,我当前的单元格按钮标题已更改为“暂停”标题,但其他一些单元格也已更改,我可以在上下滚动时看到。正如我发现的那样,我必须在tableview中保存每个单元格的状态,但我不清楚如何才能做到这一点。谁能解释一下我是怎么做到的 这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRow
- (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;
}
}
音频自定义单元类:
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
创建一个新类或将属性添加到AudioCustomCell布尔类型(播放或不播放),以保持-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath上按钮标题检查条件的状态。@SunilSingh,感谢您的回答。我在AudioCustomCell类中创建了一个属性,并在-(void)awakeFromNib方法中将其设置为NO,然后在-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath中将其签入,但它不起作用。它只是开始工作得更慢了。可能我做错了什么。请提供AudioCustomCell类的最新代码。
#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