Uitableview 单击TableViewCell内的按钮时从URL播放音频

Uitableview 单击TableViewCell内的按钮时从URL播放音频,uitableview,button,objective-c-blocks,avaudioplayer,Uitableview,Button,Objective C Blocks,Avaudioplayer,我的tableView方法是 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { wordsTableViewCell *cell; if (cell==nil) { cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; }

我的tableView方法是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    wordsTableViewCell *cell;
    if (cell==nil)
    {
        cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    }
    cell.wordIndexlbl.layer.masksToBounds = true;
    cell.wordIndexlbl.layer.cornerRadius = 8;
    cell.redWord.text= [self.wordsFromArray objectAtIndex:indexPath.row];
    cell.blueWord.text=[self.words objectAtIndex:indexPath.row];
    cell.wordIndexlbl.text=[self.wordsID objectAtIndex:indexPath.row];
    cell.playCell.tag=indexPath.row;
    [cell.playCell addTarget:self action:@selector(playURLCell:) forControlEvents:UIControlEventTouchUpInside];
    return cell;

}
然后我做了一个按钮动作和方法来调用这样的函数

- (IBAction)playURLCell:(UIButton *)sender
{
    UIButton *senderButton=(UIButton *)sender;
    NSLog(@"current row = %ld" , (long)senderButton.tag);
    [self getWord:_uppercaseString For:_combinedStirng String:wordIndex];
}
-(void)getWord:(NSString*)upperCaseString For:(NSString *)combinedString String:(NSString *)wordIndex
{
    BOOL isInternetAvailable = [[NetworkManager sharedInstance] check];

    if (isInternetAvailable)
    {

        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

            NSData *data;
            NSString *urlStr = [NSString stringWithFormat:@"http://quicklanguages.com/materiales/quicklanguages/audios/%@/%@-%@.mp3",upperCaseString,combinedString,wordIndex];
            NSURL *url = [[NSURL alloc] initWithString:urlStr];
             [playURLArray addObject:url];
            data = [NSData dataWithContentsOfURL:url];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self playAudio:data];
            });
        });
    }
}
-(void)playAudio :(NSData *)data
{
    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; // Now we are assigning it in an instance variable thus ARC will not deallocate it.

    audioPlayer.delegate=self;
    [audioPlayer play];
}
当我点击单元格内的按钮时,音频不播放,wordIndex变为零 没有错误,有什么问题,有人帮我吗


请告诉我的问题

代码中有一些概念错误。 我会给你一些建议,让你工作得更好

a) 地址:

您正在向单元格或按钮添加操作? b) 如果您有一个按钮(就像您对其他元素、标签所做的那样…),请向其添加操作

c) 最好制作一个自定义单元格(我看到您创建了一个类“wordsTableViewCell”,请在类中使用wordsTableViewCell…)并从单独的XIB加载它

d) 如果为nil,则不要有条件地调用dequeueReusableCellWithIdentifier。。(请参阅为新TableViewController创建的代码Xcode) 只需使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WordsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...  
    return cell;
}
e) 在“awakeFromNib”中进行一些准备设置和操作:

(标签将像在CellForRowatineXpath中一样填充)

f) 最好使用协议让控制器播放(或者在动作中使代码在自定义代码中,但不美观(见下文))

g) 请记住,电池是可重复使用的,所以当回收时,你会丢失所有设置

h)最后但并非最不重要的一点: 代码失败,如下所示:

-(void)playAudio :(NSData *)data
{
    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; // Now we are assigning it in an instance variable thus ARC will not deallocate it.

    audioPlayer.delegate=self;
    [audioPlayer play];
}
由于音频播放器将由ARC释放,因此音频停止。 所以,您必须保持AVAudioPlayer实例的活动状态:一个可行的解决方案是将其放入控制器中。 让我们看看步骤:

a) 比如说,创建一个协议

DIDTaptoplay协议:

@protocol DidTapToPlayProtocol <NSObject>
@required
-(void)didTapToPlay:(NSInteger)tag;
@end
d) 在按钮操作调用中:

 -(IBAction)play:(UIButton *)sender {

    NSInteger tag ... // get tag..
    [self.didTapDelegate didTapToPlay:tag];

}
e) 在表视图控制器*h中分配一个数据成员:

 @interface MyTableViewController : UITableViewController

    @property (string, nonatomic) AVAudioPlayer* audioPlayer;

@end
并在委托回调中设置:(现在将保留。注意:我们可以使用更好的方法…在ViewDidLoad中分配..无论如何..)


(我已经写了一个基本代码..如果需要,请告诉我..)

谢谢你的回复,但我已经解决了这个问题
  #import "DidTapToPlayProtocol.h"

@interface WordsTableViewCell : UITableViewCell

    @property (weak, nonatomic) id <DidTapToPlayProtocol>didTapDelegate;

@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WordsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...

    cell.didTapDelegate = self;

    return cell;
}
 -(IBAction)play:(UIButton *)sender {

    NSInteger tag ... // get tag..
    [self.didTapDelegate didTapToPlay:tag];

}
 @interface MyTableViewController : UITableViewController

    @property (string, nonatomic) AVAudioPlayer* audioPlayer;

@end
- (void)didTapToPlay:(NSInteger)tag{

    NSData * data;
    .....
    self.audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
}