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)
但它允许我选择所有细胞而不仅仅是我喜欢的一个细胞.
首先,移动 collectionView.allowsMultipleSelection = false到viewDidLoad
其次,我不认为你真的有多重选择的问题.而不是在选择时不清除放在单元格上的效果.你能做的就是清除我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)
| 归档时间: |
|
| 查看次数: |
2436 次 |
| 最近记录: |