1

I have a UICollectionView. Its cell has a UICollectionView inside of it.

enter image description here

I can detect when the cell containing the collectionView is tapped (via didSelectItemAtIndexPath). However, I cannot detect when the cells of the collectionView inside the cell (as depicted by the individual sun icons) are tapped.

User-interaction is enabled on everything. And I've tried overriding the hitTest of the collectionView as described here.

Update: I tried this again in a simple test project and it worked fine. However in my current project for some reason the inner-most nested collectionView (with the sun icons) does not appear in the view debugger.

enter image description here

Notice I made the collectionView background blue and its cells pink. And they do not appear when view debugged, although they appear in the running app?

Community
  • 1
  • 1
vikzilla
  • 3,998
  • 6
  • 36
  • 57
  • 1
    Have you set the delegate of the UICollectionViewCell that contains a UICollectionView to self? Meaning that you should subclass a UICollectionViewCell and set up the UICollectionView inside that subclass and set the delegate of that UICollectionView to the UICollectionViewCell. Also, you shouldn't need to be messing around with hit testing. Very rarely is there a case where the UIResonder chain needs some massive intervention and with this one, I doubt you need to go farther than setting delegation correctly. – Larry Pickles Aug 12 '16 at 17:39
  • @Loxx Yes, the delegate of the inner collectionView is set to the cell that it resides in – vikzilla Aug 12 '16 at 17:44
  • 1
    Hmm, let me pull up and old project and see how I worked around this. I'll post here soon – Larry Pickles Aug 12 '16 at 17:48
  • @Loxx thanks, that would be very helpful :) – vikzilla Aug 12 '16 at 17:48
  • Disable "Cancels Touches in View" for CollectionView in Storyboard. – ogres Aug 12 '16 at 18:01
  • @ogres that did not help unfortunately – vikzilla Aug 12 '16 at 18:25

2 Answers2

0

I've done this before... but it's going to be a little hard to explain. The short answer is TapGestures.

So I had the tableView through storyboard drawn. The collection view was added programatically.

For TableView cellforRow, I called UITableViewCell subclass. Which I passed the rootView as a delegate. Which called a CollectionViewClass. This is where the actual items for the cell are being drawn and added to the collection view. At this point I have the rootdelegate. For each item i add a tapGesture.

    let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped))

So each item now has a tap gesture inside it. Now in my view that was drawing i can call the rootview with which cell was tapped.

func viewTapped() {
    tapDelegate?.milestoneTapped(passID) //tapDelegate = RootView
}

I hope this helps.

afanaian
  • 1
  • 1
  • I am hoping to use the UICollectionView delegate methods rather than a workaround if possible. If I ultimately cannot use the built-in functionality I will try this – vikzilla Aug 12 '16 at 18:28
0

Once I changed from using XIBs for my cells to using regular cells from storyboard this problem did not occur.

vikzilla
  • 3,998
  • 6
  • 36
  • 57