[英]Deselect a collectionview cell when another cell is selected
I have cells with images on a viewController
, I like to give the users an option to select one of the image
for their title label
. 我在viewController
上有带图像的单元格,我想让用户选择一个image
作为title label
。 How do I make them select only one image
, that is if they select another image
I want to deselect the previous image
they selected. 如何让他们只选择一个image
,即如果他们选择另一个image
我想取消选择他们选择的上一个image
。
This is what I did: 这就是我做的:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 5.0
cell?.layer.borderColor = UIColor.black.cgColor
collectionView.allowsMultipleSelection = false
}
but it is allowing me to select all cells not just one cell which I like. 但它允许我选择所有细胞而不仅仅是我喜欢的一个细胞。
First, move collectionView.allowsMultipleSelection = false
to viewDidLoad
首先,将collectionView.allowsMultipleSelection = false
移动到viewDidLoad
Secondly, I don't think you really have a problem with multiple selection. 其次,我不认为你真的有多重选择的问题。 Rather that you don't clear the effects you put on the cell when selecting it. 而不是在选择时不清除放在单元格上的效果。 What you can do is to clear that i didDeselect
你可以做的是清除我做的didDeselect
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 5.0
cell?.layer.borderColor = UIColor.black.cgColor
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 0
cell?.layer.borderColor = UIColor.white.cgColor
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.