Amir Panahandeh
Amir Panahandeh

Reputation: 9039

didSelectItemAt for UICollectionView is only calling on double touch

I'm facing a weird issue in UICollectionView using iOS11 emulator. In my project i have a UICollectionView with UIImageView as cells and I've created segue as Triggered Segues for cells by dragging it to a view controller. It was working great but know the segue is not performing so i decided to remove the segue from cells Triggered Segues and I created a segue from my view controller to the destination view controller and performed segue from code

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
    if let url = URL(string: "\(StringResources.serverAddress)/Content/images/Files/Thumb/\(photos[indexPath.row])") {
        cell.image.downloadFrom(url: url)
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("clicked")
    performSegue(withIdentifier: "PhotoSegue", sender: collectionView.cellForItem(at: indexPath))
}

but it's not working either and there is no clicked printed in console. I've checked cell and UIImageView user interaction also UICollectionView delegate is ok. How can i fix it?


edit: I've found the problem. It's only calling when I'm double clicking on cell

Upvotes: 4

Views: 2326

Answers (3)

Amir Panahandeh
Amir Panahandeh

Reputation: 9039

the problem is that I was adding a UITapGestureRecognizer to dismiss the keyboard and that was catching touch events.

func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    view.addGestureRecognizer(tap)
}

Upvotes: 3

Haas
Haas

Reputation: 11

The issue could be due to the fact that collection views have a didSelectItemAt method as well as a didDeselectItemAt method which alternate per click. This could explain why it only works on a double click (one click calls didSelect, the next calls didDeselect).

A quick (but not elegant) solution could be to implement your didDeselectItemAt and just use it to call the didSelectItemAt

Upvotes: 1

jvrmed
jvrmed

Reputation: 834

There are some things that you could check and once all of them are in order it should work:

  1. Check that your UICollectionView has both delegate and dataSource set.
  2. Check that your UIImageView userInteractionEnabled property is set to false.

According to documentation:

Image views ignore user events by default. Normally, you use image views only to present visual content in your interface. If you want an image view to handle user interactions as well, change the value of its isUserInteractionEnabled property to true. After doing that, you can attach gesture recognizers or use any other event handling techniques to respond to touch events or other user-initiated events.

  1. Check that both UICollectionView and parent have. userInteractionEnabled property is set to true.
  2. Check that there are no other UIGestureRecognizers catching your touch event.
  3. Check that there are no network requests freezing your UI.

Considering the information that you provided, I would try to remove the UIImageView from the cell and make sure that the cell touch is working before adding more elements.

Upvotes: 9

Related Questions