didDeselectItemAtIndexPath method is not firing when I tried to select. Please let me know if there is any setting wrong in my code.
I am trying to display ticked icon if the image is selected and if deselect then disappear the image.
Thank you for your help.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
cell.imageView.image = UIImage(contentsOfFile: self.getMediaFilePath(self.mediaModels[indexPath.row].pathToMedia))
if self.mediaModels[indexPath.row].isSelected {
cell.imageTicked.hidden = false
cell.selected = true
} else {
cell.imageTicked.hidden = true
cell.selected = false
}
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
cell.backgroundColor = UIColor.magentaColor()
self.mediaModels[indexPath.row].isSelected = true
cell.selected = true
collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
cell.backgroundColor = UIColor.whiteColor()
self.mediaModels[indexPath.row].isSelected = false
cell.selected = false
collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cellSize = (collectionView.frame.width / 4) - 4
return CGSizeMake(cellSize, cellSize)
}
Try this
Use cell.userInteractionEnabled=YES; in cellForRowAtIndexPath method.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
var cell : CollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.magentaColor()
self.mediaModels[indexPath.row].isSelected = true
cell.selected = true
cell.imageTicked.hidden = false
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
var cell : CollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.whiteColor()
self.mediaModels[indexPath.row].isSelected = false
cell.selected = false
cell.imageTicked.hidden = true
}
I get it to work without reloading the data. Just set the imageTicked hidden in the didSelectItemAtIndexPath and didDeselectItemAtIndexPath method. Thanks.
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.