虽然这个问题有很多问题和答案,但没有一个能完全解释必须做什么。与Reddit upvote功能或Instagrams帖子类似,我试图点击集合视图单元格内的按钮,并在发生这种情况时更改它的按钮。
当我点击时,其他细胞会受到影响。
这是我的代码:
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.
我做错了什么导致其他单元格的按钮更改图像,尽管保存了按钮' tag为indexPath