Home > Mobile >  How do I change the Image of only 1 button in Collection View and make all other button Images diffe
How do I change the Image of only 1 button in Collection View and make all other button Images diffe

Time:06-01

In my collection view I have songs with play buttons. I want to change the image of my button upon selection and want to make the previous selected button image to revert to default image. The buttons are in an unknown amount could be anywhere between 1 to 20. Any Help would be appreciated.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    if collectionView == self.albumCollection {
    let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "album", for: indexPath) as! AlbumsCollectionViewCell
        
        cellA.albumImg.image = UIImage(named: albums[indexPath.row])
        cellA.albumLbl.text = albumLbl[indexPath.row]
        
        
    return cellA
    }
    else {
        let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "songs", for: indexPath) as! ForYouCollectionViewCell
        
        cellB.forImg.image = UIImage(named: songs[indexPath.row])

        cellB.forButton.tag = indexPath.row
        cellB.delegate = self
               return cellB
    }
    
}

and here is code for my collection view cell

import UIKit
import AVFoundation


protocol SongDelegate: AnyObject {
func songBtnTapped(sender: UIButton!)
}

class ForYouCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var forImg: UIImageView!
@IBOutlet weak var forButton: UIButton!
weak var delegate: SongDelegate?

var isTapped = false
var player: AVPlayer?
var counts: Int = 0
//test

override func awakeFromNib() {
    super.awakeFromNib()
    NotificationCenter.default.addObserver(self,selector: #selector(songBtn(_:)),name: Notification.Name("playSong"),object: nil)

}



func conditions() {
    
    
    if isTapped == false {
        forButton.setImage(UIImage(named: "wPlay"), for: .normal)
        isTapped = true


    } else {
        forButton.setImage(UIImage(named: "wPause"), for: .normal)
        isTapped = false
    }
    

    
}

@IBAction func songBtn(_ sender: UIButton) {
    delegate?.songBtnTapped(sender: sender)
    forButton.setImage(UIImage(named: ("wPause")), for: .normal)

    print(sender.tag)
    counts = sender.tag
    if counts == sender.tag {
        print(counts)
        forButton.setImage(UIImage(named: "wPlay"), for: .normal)
    } else if counts != sender.tag {
        
        forButton.setImage(UIImage(named: "wPause"), for: .normal)
    }

   
}
}

CodePudding user response:

have a variable on your collectionView ViewController that has the songPlaying: Int? and then in your cellForItemAt function you can check if indexPath.row == songPlaying show play else show pause. Have clicking on the button change the songPlaying to that tag and reload your collectionView. I am not sure where your conditions func is being used, but that seems unnecessary you should just have a buttonImage variable on the cell and set it in the cellForItemAt.

  • Related