选择其他单元格时,取消选择集合视图单元格

lea*_*122 3 ios uicollectionview swift

我有一个图像的细胞viewController,我想给用户一个选项来选择的一个image为他们title label.如何让他们只选择一个image,即如果他们选择另一个image我想取消选择image他们之前选择的.

这就是我做的:

 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
        }
Run Code Online (Sandbox Code Playgroud)

但它允许我选择所有细胞而不仅仅是我喜欢的一个细胞.

Mor*_*iya 8

首先,移动 collectionView.allowsMultipleSelection = falseviewDidLoad

其次,我不认为你真的有多重选择的问题.而不是在选择时不清除放在单元格上的效果.你能做的就是清除我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

    }
Run Code Online (Sandbox Code Playgroud)

  • 不适合我.DeselectedItem永远不会被调用. (2认同)