How to show check tick mark in Collection view (images)

Viewed 5012

In Table view we can put checkmark easily on cells.

But in Collection View how can we put check mark, when we select a cell (image)?

I just took a image view inside the cell and image view and put a tick mark image. My code is below.

But it's not working.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{

   //  handle tap events
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! customCollectionViewCell


    if(cell.checkMarkImage.hidden == true)
    {
        print("Hidden")
       cell.checkMarkImage.hidden = false

    }
    else
    {
         cell.checkMarkImage.hidden = true
        print("No Hidden")
    }
}
6 Answers

Swift 4

In ViewController

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCollectionViewCellID", for: indexPath as IndexPath) as! YourCollectionViewCell
    cell.someImageView.image = imgArr[indexPath.item]
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    print("You selected cell #\(indexPath.item)!")
    let cell = collectionView.cellForItem(at: indexPath) as? YourCollectionViewCell
    cell?.isSelected = true
    cell?.toggleSelected()    
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    let cell = collectionView.cellForItem(at: indexPath) as? YourCollectionViewCell
    cell?.isSelected = false
    cell?.toggleSelected()
}

In YourCollectionViewCell

class YourCollectionViewCell: UICollectionViewCell {


    @IBOutlet weak var someImageView: UIImageView!
    @IBOutlet weak var checkImageView: UIImageView!

    //Function  for select and deselect checkmark.
    public func toggleSelected() {

        if (isSelected == false) {

            //Hide check mark image.
            self.checkImageView.image = UIImage(named: "unCheckImage")
            isSelected = true
        }else{

            //Show check mark image.
            self.checkImageView.image = UIImage(named: "CheckImage")
            isSelected = false
        }
    }    
}

Hope enjoy!!

var selectedCellIndex:Int?

take variable if you want to show selected Item after reloadData() : which is previously selected CellItem. {inspired by above answer }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColorCollectionCell", for: indexPath) as! ColorCollectionCell

        cell.isSelected = false

        if selectedCellIndex == indexPath.item {
            cell.checkMarkImgView.image = UIImage(named: "icn_checkMark")
        }else {
            cell.toggleSelected()
        }

        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let cell = collectionView.cellForItem(at: indexPath) as! ColorCollectionCell
        cell.isSelected = true
        selectedCellIndex = indexPath.item
        cell.toggleSelected()
}

In CollectionViewCell u can use this method

class ColorCollectionCell: UICollectionViewCell {
    @IBOutlet weak var cellimgView: UIImageView!
    @IBOutlet weak var checkMarkImgView: UIImageView!

     func toggleSelected() {

        if (isSelected) {
            self.checkMarkImgView.image = UIImage(named: "icn_checkMark")
        }else{
            self.checkMarkImgView.image = UIImage(named: "")
// here you can use uncheck img here i am not using any image for not selected.
        }
    }   
}
Related