I have designed a UITableViewCell
in a .xib. The cell contains an UIImageView
that only partially covers the cell. The behavior I am seeking is, if the user taps on the UIImageView
, the UIImageView
's UITapGestureRecognizer
's selector gets triggered, and the UITableViewCell
does not trigger didSelectRowAtIndexPath
. If the user taps on the UITableViewCell
but not the UIImageView
, then I want the opposite behavior. Right know what I'm getting is, when the user taps on the UIImageView
, both the UITapGestureRecognizer
's selector and didSelectRowAtIndexPath
get triggered. And when the user taps on just the UITableViewCell
, everything works fine. How can I get the desired behavior? I have tried messing around with cancelsTouchesInView
among other things.
Asked
Active
Viewed 812 times
0
-
What happens if you add this in the method triggered by your gesture recognizer? `cell.selectionStyle = UITableViewCellSelectionStyleNone;` – Victor Engel Mar 06 '16 at 04:27
-
You may want add a `UIButton` on top `UIImageView`. – Tim007 Mar 06 '16 at 04:28
-
Actually, I think the answer to this question has your solution: http://stackoverflow.com/questions/28334216/uitableview-doesnt-get-touches-with-single-gesture-recognizer – Victor Engel Mar 06 '16 at 04:32
-
Victor - I tried that didn't work; Tim - I also have a double tap gesture recognizer I'd like to work; Victor - as far as I can tell, that question is referring to the opposite problem I'm having. That delegate method that answer is relying on doesn't seem to allow me to dictate whether didSelectRowAtIndexPath gets called. – Mar 06 '16 at 05:08
-
I'm going to try using a UITapGestureRecognizer to select the cell, I think that will work. – Mar 06 '16 at 05:14
1 Answers
1
You don't need to use gesture recognizer to the cell, instead allow userInteractionEnabled set to true for the imageView and add target to the imageView. Refer to this code:
yourImageView.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped")
yourImageView.addGestureRecognizer(tapGesture)

muazhud
- 914
- 7
- 18