1

I have a UICollectionViewController and this collection’s cells have a subview -let’s say A- with same frame as cell bounds.

The problem is when i tap on these cells, collectionView:didSelectItemAtIndexPath does not get called. I also set A’s exclusiveTouch to YES but still nothing. And also when i change A’s frame to a smaller size, tapping remaining area fires collectionView:didSelectItemAtIndexPath, no problem.

What should i actually do at that point to make this collectionView delegate get called where A’s bounds is same with cell.

I would appreciate any comments.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
cihangirs
  • 55
  • 1
  • 12

5 Answers5

5

If I understand your question correctly, you have UICollectionViewCells with subviews in it. In order to fire didSelectItemAtIndexPath for touches on subviews, you need to disable "User Interaction Enabled" in the interface builder. I have a UIButton as a subview in my collection view cell and this is what worked for me.

Muhsin Zahid Ugur
  • 533
  • 1
  • 5
  • 14
1

Setting exclusiveTouch to YES causes the receiver to block the delivery of touch events to other views in the same window. So leave it to NO

else if your subview is a UIImageView set userInteractionEnabled to YES

else if your subview is UIButton or other UIControl subclass which will capture tap gesture you need change it to plain UIView subclass

dopcn
  • 4,218
  • 3
  • 23
  • 32
  • my subview is a subclass of UIScrollView and setting userInteractionEnabled to YES does not make collectionView:didSelectItemAtIndexPath get called – cihangirs Jun 24 '15 at 08:50
  • UIScrollView will capture gesture too. It won't send touch event to next responser @cihangirs – dopcn Jun 24 '15 at 09:13
  • I set A's userInteractionEnabled to YES and exclusiveTouch to NO but still collectionView:didSelectItemAtIndexPath does not get called, anything missing or wrong here? – cihangirs Jun 24 '15 at 09:16
  • It's all about UIScrollView. It won't send touch event to next responser @cihangirs – dopcn Jun 24 '15 at 09:18
0

Check your method is it - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath or - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

It happens sometimes.

Have you disabled user interaction property of your cell check this once.

Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
0

the method should be override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) and be sure that :

  • the cell itself has user Interaction Enable checked(in your storyboard)
  • all children of this cell have also user Interaction Enable checked

You can find this checkbox in the Attribute inspector.

Mikael
  • 2,355
  • 1
  • 21
  • 45
  • i'm coding objective-c and do not use storyboard, programatically adding subviews – cihangirs Jun 24 '15 at 08:52
  • the method you implemented is the proper one: collectionView:didSelectItemAtIndexPath same signature. and for the user Interaction enable, you can simply do : yoursubview.userInteractionEnabled = YES; – Mikael Jun 24 '15 at 08:55
  • dopcn is totally right when he is saying "Setting exclusiveTouch to YES causes the receiver to block the delivery of touch events to other views in the same window. So leave it to NO" That might be one reason for your issue. – Mikael Jun 24 '15 at 08:56
0

I'm adding my stupidity answer just in case it helps someone else. In your viewcontroller remember to set the collectionview's delegate

collectionView.delegate = self
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72