I have a custom UICollectionViewCell
, it can select and deselect single cells. The problem comes when you select then deselect the cell, scroll thru the UICollectionView
and when you go back the previously selected cell shows that it's being selected. This is some reusability issue.
This is how I'm selecting and deselecting the cell:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)! as! CreateNotebookCVCell
if selectedIndexPath != indexPath || selectedIndexPath == nil {
// Select Cell
selectedIndexPath = indexPath
cell.showDimViewAndCheckmark()
selectedCover = notebookCovers[indexPath.row]
saveButton.isEnabled = true
}
else {
// Deselect Cell
selectedIndexPath = nil
cell.hideDimViewAndCheckmark()
selectedCover = nil
saveButton.isEnabled = false
}
}
And this is the code in the cell itself:
override func awakeFromNib() { super.awakeFromNib() setUI() // hideDimViewAndCheckmark() }
override func prepareForReuse() {
super.prepareForReuse()
hideDimViewAndCheckmark()
coverImageView.image = nil
}
func hideDimViewAndCheckmark() {
dimView.isHidden = true
checkmarkIcon.isHidden = true
}
func showDimViewAndCheckmark() {
dimView.isHidden = false
checkmarkIcon.isHidden = false
dimView.layer.cornerRadius = 10
dimView.clipsToBounds = true
dimView.layer.borderWidth = 2
dimView.layer.borderColor = Colors.purpleDarker.cgColor
dimView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
}
override var isSelected: Bool {
didSet {
if isSelected {
UIView.animate(withDuration: 0.1, animations: { [unowned self] in
self.showDimViewAndCheckmark()
})
}
else {
UIView.animate(withDuration: 0.1, animations: { [unowned self] in
self.hideDimViewAndCheckmark()
})
}
}
}
Not sure what I'm doing wrong here
UPDATE: cellForRowAt
method as requested
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell_Id.createNotebook, for: indexPath) as! CreateNotebookCVCell
let cover = notebookCovers[indexPath.row]
cell.coverImageView.image = UIImage(named: cover)
return cell
}
Try this
--cellForItem function
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CreateNotebookCVCell
if selectedIndexPath == indexPath{
cell.showDimViewAndCheckmark()
selectedCover = notebookCovers[indexPath.row]
saveButton.isEnabled = true
} else {
cell.hideDimViewAndCheckmark()
selectedCover = nil
saveButton.isEnabled = false
}
return cell
}
-- didSelectItemAt function
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if selectedIndexPath != indexPath || selectedIndexPath == nil {
// Select Cell
selectedIndexPath = indexPath
} else {
// Deselect Cell
selectedIndexPath = nil
}
collectionView.reloadData()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.