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:
Any help is appreciated. Thanks!