4

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

Amir_P
  • 8,322
  • 5
  • 43
  • 92

3 Answers3

9

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.

jvrmed
  • 834
  • 6
  • 12
  • it's not working. I've edited my post please read the last line – Amir_P Oct 25 '17 at 15:37
  • you need to share more information, your cell class and possibly your controller too, there is another element that is interfering in your views for sure – jvrmed Oct 25 '17 at 15:38
  • is there any other _UICollectionViewDelegate_ method that you are implementing? – jvrmed Oct 25 '17 at 15:45
  • no it's only section and item in section the cell for item and did select item – Amir_P Oct 25 '17 at 15:46
  • check this https://stackoverflow.com/questions/26351819/ios-uitableviewcell-needs-to-be-pressed-twice-to-call-didselectrowatindexpath – jvrmed Oct 25 '17 at 15:50
  • by double click i mean multi touch click @jvrmed – Amir_P Oct 25 '17 at 18:23
3

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)
}
Amir_P
  • 8,322
  • 5
  • 43
  • 92
1

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

Haas
  • 11
  • 4