问题
I have set up a gesture recognizer for my table cell in my IOS5 app:
UITapGestureRecognizer* oneFingerDoubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cellOneFingerDoubleTap:)];
oneFingerDoubleTap.numberOfTapsRequired = 2;
[cell addGestureRecognizer:oneFingerDoubleTap];
And implemented handler method:
- (void)cellOneFingerDoubleTap:(id) sender
{
NSLog(@"taptap");
}
It works fine. My problem is that I can not pass the cell was tapped or some other data with the tapped cell. As I see (id)sender is the UITapGestureRecognizer itself.
My question is: how can I get the tapped cell in the handler method (cellOneFingerDoubleTap)? How can I get in the handler method the index of the tapped cell?
Thanks!
回答1:
If you grab the view
from the gesture recogniser that is passed in to you on your cellOneFingerDoubleTap:
method, then you'll get the cell that's been tapped. Something like:
- (void)cellOneFingerDoubleTap:(UIGestureRecognizer*)recognizer {
UITableViewCell *cell = (UITableViewCell*)recognizer.view;
}
[I'm just assuming by a "cell" you mean a UITableViewCell
by the way]
回答2:
UIGestureRecognizer has a
-(UIView *)view
property that you can use to get the view to which it was attached.
Since you attached the recognizer to a UITableViewCell, pass the recognizer's view to the UITableView's;
-indexPathForCell:
来源:https://stackoverflow.com/questions/9203610/get-tapped-cell-in-uitapgesturerecognizer-handler