UITableViewCell中的UIImageView上的UITapGestureRecognizer

问题描述:

我在.xib中设计了一个UITableViewCell。该单元格包含仅部分覆盖单元格的UIImageView。我正在寻找的行为是,如果用户点击UIImageViewUIImageViewUITapGestureRecognizer的选择器被触发,并且UITableViewCell不会触发didSelectRowAtIndexPath。如果用户点击UITableViewCell而不是UIImageView,那么我想要相反的行为。知道我得到的是,当用户点击UIImageView时,UITapGestureRecognizer的选择器和didSelectRowAtIndexPath都被触发。当用户点击UITableViewCell时,一切正常。我怎样才能获得理想的行为?我曾试图用cancelsTouchesInView和其他东西搞乱。UITableViewCell中的UIImageView上的UITapGestureRecognizer

+0

如果在由手势识别器触发的方法中添加此项,会发生什么情况? 'cell.selectionStyle = UITableViewCellSelectionStyleNone;' –

+0

您可能需要在UIImageView的顶部添加一个'UIButton'。 –

+0

其实,我认为这个问题的答案有你的解决方案:http://stackoverflow.com/questions/28334216/uitableview-doesnt-get-touches-with-single-gesture-recognizer –

您不需要在单元格中使用手势识别器,而是允许为imageView设置userInteractionEnabled为true并将目标添加到imageView。请参考以下代码:

yourImageView.userInteractionEnabled = true 
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped") 
yourImageView.addGestureRecognizer(tapGesture)