2
votes

I'm working on a project where there is a UICollectionView inside of a UITableViewCell inside of a UITableView. I used a XIB for my UICollectionViewCell which contains an ImageView, and another XIB for my UITableViewCell which contains a UICollectionView. I have managed to display the required things, however, my custom collection view cells are not responding to any touch event. After some research, I have tried:

  • Turning OFF "User Interaction Enabled" in the UITableView & UITableViewCell in the Interface Builder and programatically, while turning ON "User Interaction Enabled" in both the UICollectionView and UICollectionViewCell
  • Setting the delegates for the UICollectionView inside the UITableViewCell, where my custom UITableViewCell class implements the UICollectionViewDelegate and UICollectionViewDataSource

     collectionView.delegate = self
     collectionView.dataSource = self
    
  • Using the delegate methods of UICollectionView

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // my implementation here
    }
    
  • Adding a custom UITapGestureRecognizer to each cell in cellForItemItemAt

     let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyCustomTableViewCell.cellTapped))
     tapRecognizer.numberOfTapsRequired = 1
     cell.addGestureRecognizer(tapRecognizer) 
    
  • Using a custom implementation of UITableViewCell, UICollectionView, and UICollectionViewCell , overriding hitTest function

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let hitView = super.hitTest(point, with: event) {
            if hitView is MyCustomCollectionViewCell {
                print("hitTest - MyCustomCollectionViewCell")
                return hitView
            } else {
                return nil
            }
        } else {
            return nil
        }
    }
    

Here is a screenshot of my custom TableViewCell XIB: enter image description here

Any help is appreciated. Thanks!

2
Try to Give Fix Height to collection View.Ujesh
Use Collection View delegate method instead of tap gesture.Ujesh
@Ujesh they do have a fixed height, but I don't see how that helpsmugiwara528
Here is tutorial for 'UICollectionView' inside 'UITableView' : ashfurrow.com/blog/…yerpy
@Ujesh I forgot to mention it in original post, but I did that. See edited versionmugiwara528

2 Answers

4
votes

Turns out, TableViewCells now have contentViews which blocked all my touches inside. So, I simply used this in my UITableViewDelegate -

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ....
    cell.contentView.isUserInteractionEnabled = false
    ....
}

For reference: can't click UIButton inside a cell in UITableview

Shout out to: Stas Volskiy , for being very helpful

1
votes

Probably you've set something wrong. User interaction should be enabled for tableView, tableViewCell, collectionView, collectionViewCell. If you turn of it for tableView - then everything inside it will not be interactive. Check out my sample project here where it is working: https://github.com/1mperial8e/CollectionViewExample

I've recreated same situation that you have - collectionView with imageView in cell inside tableViewCell. There is nothing extra, just simple views. You can compare settings and find where is the problem in your code or storyboard settings.

You don't need any tap gestures. collectionView:didSelectItemAtIndexPath: works perfect.