While there are many questions and answers to this subject, none of them fully explain what has to be done. Similar to the Reddit upvote feature or Instagrams post, I'm trying to click a button inside a collection view cell and have it's button changed when such happens.
When I do click, other cells are affected.
Here is my code:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cartBookView", for: indexPath) as! CartBookPreviewCell
cell.saveButton.tag = indexPath.row
cell.saveButton.addTarget(self, action: #selector(self.clickLike(sender:)), for: .touchUpInside)
return cell
}
func clickLike(sender: UIButton) {
let index: Int = sender.tag
if lib.Library[index].saved {
sender.setImage(#imageLiteral(resourceName: "Vector"), for: .normal)
lib.Library[sender.tag].saveBook(save: false)
}
else {
sender.setImage(#imageLiteral(resourceName: "Vector-Saved"), for: .normal)
lib.Library[sender.tag].saveBook(save: true)
}
print(index)
print(sender.tag)
}
//"Lib" is an object of a Book Collection Class that Holds an array Books. One of the attributes of the Book Class is a Boolean called save.
What am I doing wrong that makes causes other cell's button to change images despite saving the buttons' tag as indexPath