【发布时间】:2020-04-16 08:17:04
【问题描述】:
以下是我的 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")
}
现在 didDeselectItemAt 不会被调用,如果我选择一个项目并选择另一个项目,或者如果我选择相同的项目 didDeselectItemAt 根本不会被调用,它只是在调用 didSelectItemAt 方法为什么?如何解决这个问题?
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewcell