I'm trying to implement a quite simple TableView, in which I got a Prototype Cell containing a Heart-Shaped Button. When I click on that Button, I want to change that Buttons color (precisely: the Image of the Button) to be filled.
It actually works, but not only the selected Cell fills its Heart Button, but every 4th Cell does so and I have no clue why and how to change that.
Here's the cellForRowAt method, where I access the Cells' Heart Button and define the target for when its clicked:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomCell
[...]
if let likeButton = cell.likeButton {
likeButton.tag = indexPath.row
likeButton.addTarget(self, action: #selector(likeButtonClicked(sender:)), for: .touchUpInside)
}
cell.initUI()
return cell
}
... and here's the target method "likeButtonClicked":
@objc func likeButtonClicked(sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
sender.setImage(UIImage(named: "like-icon-filled.png"), for: .normal)
} else {
sender.setImage(UIImage(named: "like-icon-empty.png"), for: .normal)
}
}
Thinking logically, there must be multiple senders at once, that are changed in this method, but I don't know, please help me out.