以下是我的CollectionView代码
categoryCollectionView.delegate = self
categoryCollectionView.dataSource = self
categoryCollectionView.allowsSelection = true
categoryCollectionView.allowsMultipleSelection = false
下面是UICollectionViewCell代码
class AppPageCategoryViewCell: UICollectionViewCell {
var catgory : String?
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var closeImageView: UIImageView!
@IBOutlet weak var stackContainer: UIView!
var facet : Facets?
func setUI() {
titleLbl.text = facet?.name ?? ""
let isSelected = facet?.isSelected ?? false
stackContainer.layer.borderColor = UIColor.black.cgColor
stackContainer.backgroundColor = isSelected ? UIColor.black : UIColor.white
titleLbl.textColor = isSelected ? UIColor.white : UIColor.black
closeImageView.image = closeImageView.image?.withRenderingMode(.alwaysTemplate)
closeImageView.tintColor = UIColor.hexStringToUIColor(hex: AppStrings.whiteColor)
stackContainer.layer.cornerRadius = 16
stackContainer.layer.borderWidth = 1
closeImageView.isHidden = !isSelected
}
}
以下是选择确定方法
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("deselect----------deselect")
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("select----------select")
}
Now didDeselectItemAt不会被调用,如果我选择一个并选择另一个项目,或者如果我选择相同的项目didDeselectItemAt根本没有被调用,那只是在调用didSelectItemAt方法,为什么?该如何解决?
此委托方法未触发,因为您将布尔值“ allowsMultipleSelection”设置为false。允许取消选择该项必须为true。
https://developer.apple.com/documentation/uikit/uitableview/1614938-allowsmultipleselection