2

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!

mugiwara528
  • 381
  • 5
  • 14
  • Try to Give Fix Height to collection View. – Ujesh Aug 17 '17 at 07:15
  • Use Collection View delegate method instead of tap gesture. – Ujesh Aug 17 '17 at 07:16
  • @Ujesh they do have a fixed height, but I don't see how that helps – mugiwara528 Aug 17 '17 at 07:17
  • 2
    Here is tutorial for 'UICollectionView' inside 'UITableView' : https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/ – yerpy Aug 17 '17 at 07:24
  • @Ujesh I forgot to mention it in original post, but I did that. See edited version – mugiwara528 Aug 17 '17 at 07:24
  • Hey @mugiwara528.. You can also try these 2 tutorials : - https://contentpedlar.wordpress.com/2016/10/22/uicollectionview-in-uitableview/ https://medium.com/@adinugroho/fixed-height-uicollectionview-inside-uitableview-79f24657d08f – Megha_Singh Aug 17 '17 at 07:32

2 Answers2

4

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

mugiwara528
  • 381
  • 5
  • 14
1

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.

Stas Volskiy
  • 441
  • 2
  • 5
  • Hello, thanks for your answer. However, as stated in my post, I am using XIB to dynamically create my UITableViewCell with the UICollectionView (whose cells are also created from XIB), unlike yours which the cell and collection view and collection view cells are created in the Storyboard. I also enabled all user interaction as suggested, but it still doesn't work. – mugiwara528 Aug 17 '17 at 08:55
  • I'll update test project with using XIB cells. Are you ok with objective-c code sample or should I write in swift? – Stas Volskiy Aug 17 '17 at 09:22
  • 1
    @mugiwara528 updated. Now cells are created in xib files. Checkout new version from git – Stas Volskiy Aug 17 '17 at 09:35
  • Hello! I followed your project in detail, but it still doesn't work for me! I have noticed that when I click on my cell, it gets highlighted in gray, but yours doesn't. Does that mean anything? – mugiwara528 Aug 17 '17 at 10:52
  • hmm.. I guess your TableViewCell become selected and that's why event is not triggered to CollectionView. Try to turn of selection of your TableView. In mine example collectionView covers whole size of tableView cell and that's why it is not selecting. Let me know if it helps, I'll update answer then – Stas Volskiy Aug 17 '17 at 11:01
  • I turned off selection, but still nothing. I have 2 UILabels in my tableView cell – mugiwara528 Aug 17 '17 at 11:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152143/discussion-between-stas-volskiy-and-mugiwara528). – Stas Volskiy Aug 17 '17 at 11:15