简体   繁体   English

更改在UITableview中按下的特定按钮的图像

[英]Change the image of a specific button pressed in a UITableview

I have a dynamic tableview which has a prototype cell with a button that, when pressed, plays a song. 我有一个动态表格视图,其中包含一个带有按钮的原型单元,当按下按钮时,该按钮会播放一首歌曲。

I'd like for the background image on the button to change to a "stop" icon when the user presses the button to play the song, and for it to change to a "play" icon when the user presses it once again to stop the song. 我希望按钮的背景图像在用户按下按钮播放歌曲时变为“停止”图标,并且在用户再次按下它停止播放时变为“播放”图标这首歌曲。

To accomplish this I've been trying to use: [self.beatPlayButton setBackgroundImage:[UIImageimageNamed:@"play.png"]forState:UIControlStateNormal]; 为此,我一直在尝试使用: [self.beatPlayButton setBackgroundImage:[UIImageimageNamed:@"play.png"]forState:UIControlStateNormal];

My issue is that I haven't figured out how to change only the button in the row that is being pressed since I'm using prototype cells. 我的问题是,自从使用原型单元以来,我还没有想出如何只更改被按下的行中的按钮。 I've been hoping to find a method like didSelectObjectAtRow:atIndexPath: because didSelectRowAtIndexPath: will fire when the row is pressed, but not a button on it (unless I'm mistaken). 我一直希望找到类似didSelectObjectAtRow:atIndexPath:的方法,因为didSelectRowAtIndexPath:会在按下该行时触发,但不会在按下该按钮时触发(除非我弄错了)。 Maybe I should be using a tag? 也许我应该使用标签? I'm not sure. 我不确定。 Any pointers would be greatly appreciated. 任何指针将不胜感激。

EDIT: My code is outside of didSelectRowAtIndexPath . 编辑:我的代码不在didSelectRowAtIndexPath之外。

Here is sample code - 这是示例代码-

- (IBAction)playStopBeat:(UIButton*)sender event:(id)event {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    if([self.audioPlayer isPlaying])
    {
        [self.beatPlayButton setBackgroundImage:[UIImage imageNamed:@"play.png"]forState:UIControlStateNormal];
        [self.audioPlayer stop];
        self.isPlaying = NO;
        self.audioPlayer = nil;
    }
    else {

        if (indexPath.row == 0) {
            [self.beatPlayButton setImage:[UIImage imageNamed:@"stop.png"] forState: UIControlStateNormal];
        }
        else if (indexPath.row == 1){
            [self.beatPlayButton setBackgroundImage:[UIImage imageNamed:@"stop.png"]forState:UIControlStateNormal];
        }
        else if (indexPath.row == 2){
...
//code to play song

Use tags, like you suggested. 按照您的建议使用标签。 In your cellForRowAtIndexPath method give each cell a tag: 在您的cellForRowAtIndexPath方法中,为每个单元格添加一个标签:

cell.tag = indexPath.row;

also set each target on the cells: 还要在单元格上设置每个目标:

[cell.playButton addTarget:self action:@selector(play:)forControlEvents:UIControlEventTouchUpInside];

Then in your play method, use your tag for your array or w/e: 然后在您的播放方法中,为您的数组或w / e使用标签:

- (IBAction)play:sender
{
     UITableViewCell *cell = (UITableViewCell *)sender;
     NSLog(@"tag = %d", cell.tag);

     if(tag == 0)
     {
          //you could also use a switch statement 
     } else if(tag == 1) {

     }
}

::EDIT:: (answer to comment) :: EDIT::(评论的答案)

To disable the other buttons, in your play method: 要禁用其他按钮,请在您的播放方法中:

- (IBAction)play:sender
{
     .........
     ....other code....

     UITableViewCell *cell = (UITableViewCell *)sender;

     for(int i = 0; i < [dataArray count]; i++)
     {
        UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

        if(tempCell.tag != cell.tag)
        {

            [cell.playButton setEnabled:NO];
        }
     }
}

In doing this though, after the song is done playing you must set them all back to enabled. 不过,在执行此操作后,必须在将歌曲全部播放完毕后将它们全部重新设置为启用。 This would be done in the MediaPlayers delegate method: didFinishPlaying. 这将在MediaPlayers委托方法:didFinishPlaying中完成。

Since you're using target/action, the sender argument will contain the button that was tapped. 由于您使用的是目标/操作,因此sender参数将包含被点击的按钮。 Instead of referring to the button as self.beatPlayButton, just use sender, so [sender setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; 不用将按钮称为self.beatPlayButton,而是使用sender,因此[sender setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM