Ralph Ralph - 6 months ago 20
iOS Question

UICollectionView Cell with tick box in the image

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{

var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
println(indexPath)
cell.imageView.contentMode = .ScaleAspectFill
cell.backgroundColor = UIColor.whiteColor()
cell.layer.borderColor = UIColor.blackColor().CGColor
cell.layer.borderWidth = 0.1
cell.frame.size.width = self.screenWidth / 3
cell.frame.size.height = self.screenWidth / 3


let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as! String
cell.imageView.image = UIImage(contentsOfFile: self.getMediaFilePath(self.mediaModels[indexPath.row].pathToMedia))
cell.imageTicked.hidden = true

return cell

}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1
}

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
}
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
}


I can hide imageTicked in the function which is returning the cell but I cannot add it inside
didSelectItemAtIndexPath
and
didDeselectItemAtIndexPath
.
I would like to show the tickbox when the item is selected and hide the tickbox when the item is deselected.

Just wondering how to set
cell.imageTicked.hidden = true
inside
didDeselectItemAtIndexPath
and
cell.imageTicked.hidden = false
inside
didSelectItemAtIndexPath
.

Thank you very much for your help

Answer

You just need to create a selectedIndexPath variable. Set it in didSelectItemAtIndexPath and remove in didDeselectItemAtIndexPath. Remember to call reloadData() to reload collection view.

var selectedIndexPath: NSIndexPath?;
...         
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath: NSIndexPath){
   ...
   if selectedIndexPath != nil {
      cell.imageTicked.hidden = selectedIndexPath.row == indexPath.row
   }
   else {
      cell.imageTicked.hidden = true
   }
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
   ...
   self.selectedIndexPath = indexPath
   collectionView.reloadData()
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
  ...
  self.selectedIndexPath = nil
  collectionView.reloadData()
}
Comments