Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 995
Amir_P Avatar asked Dec 11 '22 09:12

Amir_P


1 Answers

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.

like image 59
jvrmed Avatar answered Feb 15 '23 09:02

jvrmed