Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selection is not possible when UICollectionView is nested in UITableVIew

I have a UITableView where each cell contains a UICollectionView.

I can scroll the UITableView vertically and scroll the nested UICollectionView horizontally, however I cannot select a UICollectionViewCell in the UICollectionView.

Selection is disabled in the UITableView, and enabled (the default state) in the UICcollectionView.

The UICollectionView's collectionView:didSelectItemAtIndexPath: is simply never called.

like image 243
John Cromartie Avatar asked Sep 14 '25 03:09

John Cromartie


1 Answers

The way I was able to solve this was to add a tap gesture recognizer to the cell to handle the tap manually, rather than rely on the didSelectRowAtIndexPath which does not get called:

Swift

let tapRecognizer = UITapGestureRecognizer(target: self, action: "cellTapped:")
tapRecognizer.numberOfTapsRequired = 1
cell.addGestureRecognizer(tapRecognizer)

Objective-C

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[cell addGestureRecognizer:tapRecognizer];

You can now handle the cell being tapped in the cellTapped: method, and you can get the reference to the cell that was tapped via tapRecognizer.view.

like image 129
Jonathan Ellis Avatar answered Sep 15 '25 16:09

Jonathan Ellis