播放完成后更改button图像

当我单击播放button时,发件人button发生变化,但是当我在桌面视图中单击另一个button时,button第一个button图像不会变为播放

@IBAction func playSound(sender: UIButton) { if self.player != nil && self.player.playing { player.stop() sender.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal) } else { sender.setImage(UIImage(named: "pause.png"), forState: UIControlState.Normal) } } 

在这里输入图像说明

 **Try this code ...** Declare one variable for get index. var songnamearray : NSArray = NSArray() var audioPlayer = AVAudioPlayer() var selectedindex : Int? var isFristtime : Bool = false override func viewDidLoad() { super.viewDidLoad() songnamearray = ["Saiyaan (Sanam) 320Kbps(MajMasti.in)" ,"Phone Mein Teri Photo (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Mile Ho Tum Humko (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ," Rain Mashup (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Car Mein Music Baja (Neha Kakkar) 320Kbps"] } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return songnamearray.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Songcell", forIndexPath: indexPath) // info 5 play 6 name 7 let eventname: UILabel = (cell.viewWithTag(7) as! UILabel) let info: UIButton = (cell.viewWithTag(5) as! UIButton) let play: UIButton = (cell.viewWithTag(6) as! UIButton) if selectedindex == indexPath.row{ if isFristtime == true { play.setImage(UIImage(named: "pause-button.png"), forState: UIControlState.Normal) }else{ play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal) } }else{ play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal) } info.setImage(UIImage(named: "icon (5).png"), forState: UIControlState.Normal) play.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown) info.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown) eventname.text = songnamearray.objectAtIndex(indexPath.row) as? String return cell } @IBAction func CloseMethod(sender: UIButton, event: AnyObject) { let touches = event.allTouches()! let touch = touches.first! let currentTouchPosition = touch.locationInView(self.Songlisttable) let indexPath = self.Songlisttable.indexPathForRowAtPoint(currentTouchPosition)! selectedindex = indexPath.row if isFristtime == true { audioPlayer.pause() isFristtime = false }else{ let songnamestring : NSString = songnamearray.objectAtIndex(indexPath.row) as! String do { if let bundle = NSBundle.mainBundle().pathForResource(songnamestring as String, ofType: "mp3") { let alertSound = NSURL(fileURLWithPath: bundle) try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) audioPlayer.prepareToPlay() audioPlayer.play() } } catch { print(error) } isFristtime = true } self.Songlisttable.reloadData() } 

尝试设置button的背景图像

 let image = UIImage(named: "play.png") as UIImage sender.setBackgroundImage(image, forState: UIControlState.Normal) 

希望这可以帮助。

这是电话单选button。 我从来不使用单元格中的单选button。 这对我来说很难。 我是新手。 如果U需要它,那就是: UItableViewCells中的单选button逻辑 。 但我使用其他方式:didSelectRowAtIndexPath。 当你点击单元格,button当前是显示暂停图像,其他button是显示播放图像。 你可以参考它

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ currentIndexPath = indexPath; YourTableViewCell *cell = nil; if (!previousIndexPath) { previousIndexPath = currentIndexPath; } if (previousIndexPath != currentIndexPath) { cell = (YourTableViewCell *) [_yourTableView cellForRowAtIndexPath:previousIndexPath]; [cell.radioBtn setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal]; cell = (YourTableViewCell *) [_yourTableView cellForRowAtIndexPath:currentIndexPath]; [cell.radioBtn setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal]; previousIndexPath = currentIndexPath; } if (previousIndexPath == currentIndexPath) { cell = (YourTableViewCell *) [_yourTableView cellForRowAtIndexPath:previousIndexPath]; [cell.radioBtn setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal]; } }