1

I have a collection view where when each cell is tapped a larger version of the cell image pops up and disappears when tapped again. On top of this I'd like to be able to select a view in the corner of the cell that displays a blue checkmark(SSCheckMark View) or a greyed out checkmark when tapped again. My current code is:

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

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

    cell.backgroundColor = .clear
    cell.imageView.image = UIImage(contentsOfFile: imagesURLArray[indexPath.row].path)
    cell.checkmarkView.checkMarkStyle = .GrayedOut
    cell.checkmarkView.tag = indexPath.row
    cell.checkmarkView.checked = false

    let tap = UITapGestureRecognizer(target: self, action: #selector(checkmarkWasTapped(_ :)))
    cell.checkmarkView.addGestureRecognizer(tap)

    return cell
}

func checkmarkWasTapped(_ sender: SSCheckMark) {

    let indexPath = IndexPath(row: sender.tag, section: 1)

    if sender.checked == true {
        sender.checked = false
    } else {
        sender.checked = true
    }
    collectionView.reloadItems(at: [indexPath])
}

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

    addZoomedImage(indexPath.row)
    addGestureToImage()
    addBackGroundView()

    view.addSubview(selectedImage)
}

But when I run and select the checkmark view I get the error:

unrecognized selector sent to instance on the first line of checkmarkWasTapped() i can see that it doesn't like sender but i don't know why. Any help would be great.

Community
  • 1
  • 1
Wazza
  • 1,725
  • 2
  • 17
  • 49

1 Answers1

1

UITapGestureRecognizer tap's sender is the gesture. The checkmarkWasTapped method definition is wrong. And you can get the checkmarView using sender.view. Try this.

func checkmarkWasTapped(_ sender: UIGestureRecognizer) {

    let checkmarkView= sender.view as? SSCheckMark

    let indexPath = IndexPath(row: checkmarkView.tag, section: 1)

    if checkmarkView.checked == true {
        checkmarkView.checked = false
    } else {
        checkmarkView.checked = true
    }
    collectionView.reloadItems(at: [indexPath])
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • perfect thankyou. Do you know why its taking two taps for the check to appear and dissapear? – Wazza Mar 25 '17 at 21:12