I have a UITableViewCell with a UICollectionViewCell inside. I want the user to be able to scroll the UICollectionViewCell but when he taps anywhere inside the UITableViewCell I need it to be selected. Currently when the user taps the UITableViewCell outside the UICollectionViewCell it is selected properly, but when he taps inside the UICollectionViewCell nothing happens. My idea is to implement the collectionView: didSelectItemAtIndexPath: method inside the UITableViewCell and programmatically trigger a "self selection", but I can't seem to find a way to do this. If I store a reference to the table and the indexPath of the cell inside itself I will probably be able to do it, but I have a feeling that this would be a bad way of doing it. How to do this properly?
My guess is that the UITableViewCell's didSelectRowAtIndexPath: is blocking the UICollectionViewCell's collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:
I would first check if that's the case by putting breakpoints for the UITableViewCell's didSelectRowAtIndexPath: while selecting the UICollectionViewCell.
If that's the case, this answer might help you (comes with a nice tutorial too): https://stackoverflow.com/a/17120673/5465258
I think they have a similar problem to what you had.
Well, I found a solution for this, though I don't know if it's the best one.
First, I added a UITapGestureRecognizer
to the UICollectionView
inside my UITableViewCell
, to override the component's one.
-(void)awakeFromNib
{
...
UITapGestureRecognizer *v_CollectionViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCollectionView)];
[self.m_CollectionView addGestureRecognizer:v_CollectionViewTap];
...
}
Second I created a block property to hold the code for the programatic selection of a row and created a method that calls this block, setting it as the action for the previously created UIGestureRecognizer
.
typedef void (^OnClickTableViewCellBlock)();
@property (nonatomic, copy) OnClickTableViewCellBlock m_OnClickBlock;
Last, when I'm creating the UITableViewCell
, in the tableView:cellForRowAtIndexPath:
method, I pass a block that calls the tableview:didSelectRowAtIndexPath:
one.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
v_Cell.m_OnClickBlock = ^void()
{
[tableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
};
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With