Change icon button in tableView

I have an audio player in the tableView that works with the help of a button. The question is: how to change the icon of a button when you click on it. I tried to make it many days but everything is without effect

class TableViewControllerAudioList: UIView,UITableViewDelegate,UITableViewDataSource {

  @IBOutlet weak var tableView: UITableView!

   var player:AVAudioPlayer!

   override func setNeedsDisplay() {


 func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
   return 1

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
    return modalsF.count

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellAudioList = modalsF[indexPath.row].AudioName
cell.duration.text = "00:00"
cell.number.text = "\(indexPath.row + 1)"

cell.tapAction = { (cell) in

    self.player = self.setupAudioPlayerWithFile(file: "" + modalsF[indexPath.row].UrlName! as NSString, type: "mp3")

 return cell

 func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer?  {

let url = URL(string: file as String)
var audioPlayer:AVAudioPlayer?

do {
    let data = try Data(contentsOf: url!)
    audioPlayer = try AVAudioPlayer(data: data)

} catch let error as NSError{



return audioPlayer


class TableViewCellAudioList: UITableViewCell {

@IBOutlet weak var number: UILabel!
@IBOutlet weak var name: UILabel! 
@IBOutlet weak var duration: UILabel!
@IBOutlet weak var playChange: UIButton!

var tapAction: ((UITableViewCell) -> Void)?
@IBAction func playAudio(_ sender: Any) {



1 answer

  • answered 2017-06-17 18:52 ramzesenok

    Try to play with button.setImage(yourIcon, forState: .normal). Do it in a button's function or in tableView(:didSelectRowAt:)