DidSelectItemAtIndexPath not called with multiple collection views in one screen

I have 2 collection views on one screen, and I have a data source and a delegate implemented for both in the same controller. However, delegate methods such as didSelectItemAtIndexPath are called only for it.

Additional Information:

  • Both collection views have a cell with an image.
  • AllowSelection and UserInteractionEnabled are set to true in both views of the collection.
  • User engagement enabled on images
  • The delegate and data source are set in the storyboard from both collection views to the view controller.
  • Collection views are displayed correctly.

Both collection views have the same setting, but only one delegate works. Do you have any hints that could be customized?

Collection views are inside a scroll view. Could this be somehow related?

EDIT 2:

Project with the same problem: https://github.com/iagomr/ProblemWithAutoLayout

EDIT 1:

Somehow this is due to the limitations of auto-detection, because if I attach the bottom view of the collection to the bottom of the screen, and not to the bottom of another collection view, it will start working.

All this is due to the fact that I need to build a high screen and add everything to the view as a scroll of 1000 points.

the code:

//MARK: - CollectionView Delegate func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { println("Called") } //MARK: - CollectionView DataSource func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if collectionView == thisCV { return 1 } else if collectionView == thisOtherCV{ return 1 } } func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { if collectionView == "thisCV" { if let thisCell = collectionView.dequeueReusableCellWithReuseIdentifier("thisCell", forIndexPath: indexPath) as? thisCollectionViewCell { thisCell.image = image return thisCell } } else if collectionView == "thisOtherCV"{ if let thisOtherCell = collectionView.dequeueReusableCellWithReuseIdentifier("thisOtherCell", forIndexPath: indexPath) as? OtherCollectionViewCell { thisOtherCell.image = image return thisOtherCell } } return UICollectionViewCell() } 
+4
source share
1 answer

I can confirm that didSelectItem not called. If the constant for a top-down constraint between two views of a collection changes from 501 to 0, it works.

This problem is most likely due to the fact that you have two types of scrolling (collection types) inside another type of scrolling. In general, I would say that you should change your interface. I would suggest two ways to fix it.

Using a single collection view

Use only one collection view with different sections for different content. Also, DO NOT paste it into the scroll view. The collection view already has scrolling so you can scroll easily. You can also delete a different cell class for different partitions so that you can do whatever you want to do now.

If you want a starting point, here is a good guide that should help you with this.

Using a scroll view

If you want to customize your interface in Interface Builder, delete both collection views and just add your entire user interface inside the scroll. Place the UIButton where you want to click to complete the action.

You can even assign the same action to each button, and then distinguish which one was triggered by assigning custom tags to each of them.

+2
source

All Articles