简体   繁体   English

UITableViewCell子视图不接收触摸

[英]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 我的可重用单元格包含一个视图及其所有信息( UIImageViewUILabel等),其框架为0,0,320,63 ,名为mainView

I also have another subview, added programmatically, with a frame of -160,0,160,63 , named leftView , contained in mainView . 我还以编程方式添加了另一个子视图,该子视图的框架-160,0,160,63名为leftView ,包含在mainView

I added a UISwipeGestureRecognizer to the cell, so when you swipe, I change the frame of mainView to 160,0,320,63 . 我向该单元格添加了一个UISwipeGestureRecognizer,因此,当您滑动时,我将mainView的帧更改为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. 我只是有一个问题,在leftView ,我有一些UIImageView其中userInteractionEnabled设置为YES ,上面有一个手势识别器。 But this gesture recognizer is never fired, it still calls the -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method. 但是此手势识别器永远不会触发,它仍会调用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法。

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. 没有看到peerRecognizer的初始化,可能是很多问题。 Two main things to look into however: 但是,有两件事需要研究:

1.) Have you set the cancelsTouchesInView flag? 1.)您是否设置了cancelsTouchesInView标志? Normally this would be messing with your tableView touches, but you should look into it though. 通常,这会弄乱您的tableView接触,但是您应该对其进行调查。

2.) Have you verified the subview layout inside of storyboard? 2.)您是否验证了情节提要中的子视图布局? Is there something blocking part of your imageView? 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). 问题是因为leftView框架不在mainView框架(x中为-160)之外。

To solve my problem, I changed mainView in storyboard, so that it also starts at -160. 为了解决我的问题,我在情节mainView中更改了mainView ,因此它也从-160开始。 And instead of adding leftView at -160 in x, I add it at 0. 而不是在x的-160处添加leftView ,而是在0处添加。

Thanks anyway ! 不管怎么说,还是要谢谢你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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