简体   繁体   中英

UITableViewCell subview does not receive touch

My reusable cell contains a view with all its informations ( UIImageView , UILabel , etc.) with a frame of 0,0,320,63 , named mainView

I also have another subview, added programmatically, with a frame of -160,0,160,63 , named leftView , contained in mainView .

I added a UISwipeGestureRecognizer to the cell, so when you swipe, I change the frame of mainView to 160,0,320,63 . It works perfectly.

I just have a problem, in leftView , I have some UIImageView with userInteractionEnabled set to YES , with a gesture recognizer on it. But this gesture recognizer is never fired, it still calls the -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.

I looked at this SO answer , but it does not help me.

Would somebody help me with that ?

Thank you !!

EDIT : Here is my gesture recognizer instantiation

// Selector is valid, I checked it out. _viewController too.
UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setImage:someImage];
[imageView setUserInteractionEnabled:YES];
[_leftView addSubview:imageView];
UITapGestureRecognizer * gr = [[UITapGestureRecognizer alloc] initWithTarget:_viewController action:selector];
[imageView addGestureRecognizer:gr];

Without seeing the gestureRecognizer's initialization, it could be a number of things that are the problem. Two main things to look into however:

1.) Have you set the cancelsTouchesInView flag? Normally this would be messing with your tableView touches, but you should look into it though.

2.) Have you verified the subview layout inside of storyboard? Is there something blocking part of your imageView?

-Also you've actually allocated the gestureRecognizer correct?

Have you tried to set table cell selectable to No? in this case your swipe gesture should still work, but table deligate method for cell selection shouldn't.

I figured it out myself.

The problem was because the leftView frame was outside of the mainView frame (-160 in x).

To solve my problem, I changed mainView in storyboard, so that it also starts at -160. And instead of adding leftView at -160 in x, I add it at 0.

Thanks anyway !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM