I have a button which works as favorite. This button is inside the collection view.The button has 2 states.one selected and unselected.The images changes accordingly for the button.
I have achieved the image change for the button inside collection view.
The code is given below:
@objc func favBtnAction(_ sender: UIButton) {
// print("inside favbtnaction",sender.tag,self.likedIdArray,self.finalidArray1)
if boolarray1[sender.tag]{
print("1")
let userid = String(describing: self.finalidArray1[sender.tag])
boolarray1[sender.tag] = false
if let image = UIImage(named: "fav") {
sender.setImage(image, for: .normal)
}
removefavservice(id:userid)
}else{
print("2")
let userid = String(describing: self.finalidArray1[sender.tag])
boolarray1[sender.tag] = true
if let image = UIImage(named: "fav_filled") {
sender.setImage(image, for: .normal)
}
addfavservice(id:userid)
}
}
Inside the collection view,the code for favorite button is as follows:
cell.favBtn.tag = indexPath.row
cell.favBtn.isUserInteractionEnabled = true
cell.favBtn.addTarget(self, action: #selector(favBtnAction), for: .touchUpInside)
if boolarray1[indexPath.row]{
if let image = UIImage(named: "fav_filled") {
cell.favBtn.setImage(image, for: .normal)
}
}else{
if let image = UIImage(named: "fav") {
cell.favBtn.setImage(image, for: .normal)
}
}
But when i goto another view controller and come back to same view controller, the state is not maintained.How to achieve this?Please help me out.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…