7

I have a UITableview. One of the UITableViewCell's is a UICollectionview with instance name "amenityView". The UICollectionViewDelegate and the UICollectionViewDataSource are set in the storyboard as shown below. The following methods get called and the data is populated as expected.

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collection cellForItemAtIndexPath:(NSIndexPath *)indexPath

enter image description here

However, the methods below didn't get called when I select the UICollectionViewCell contained in the UICollectionView. What have I missed?

-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

UPDATE: return YES in this method collectionView:shouldSelectItemAtIndexPath: will invoke the below two methods. At least that's what was missing on my part. Hope this will help some body... enter image description here

MobileDev
  • 3,750
  • 4
  • 32
  • 35
  • 1
    is ["`.allowsSelection`"](https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instp/UICollectionView/allowsSelection) for your collection view set to YES? – Michael Dautermann May 15 '14 at 20:40
  • @MichaelDautermann. Thanks for your response. ".allowsSelection" is set to "YES". – MobileDev May 15 '14 at 20:45
  • does the table view cell it's sitting in allow user interaction or selection of the cell itself? – Michael Dautermann May 15 '14 at 20:48
  • the `UICollectionView` is smaller than the table view cell. When I select the area outside the `UICollectionView` but inside the table view cell the method `didSelectRowAtIndexPath` gets called. So yes the selection of the cell works. – MobileDev May 15 '14 at 20:54

1 Answers1

1

Does the table view's didSelectRowAtIndexPath get called? If so, the table view may be intercepting the touches and not passing them through to the collection view inside. I've never done the solution you are trying to do, but the collection view is inside a scroll view, and it may not be easy to pass the touch info along to the collection view in a way that makes sense, so you may need to disable tapping on the table view cell in order for the collection view to respond to touches.

BJ Miller
  • 1,536
  • 12
  • 16