xcode 在 UITapGestureRecognizer 处理程序中获取点击单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9203610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get tapped cell in UITapGestureRecognizer handler
提问by Tom
I have set up a gesture recognizer for my table cell in my IOS5 app:
我在我的 IOS5 应用程序中为我的表格单元格设置了一个手势识别器:
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.
它工作正常。我的问题是我无法通过被点击的单元格或其他一些带有被点击单元格的数据。正如我所见,(id)sender 是 UITapGestureRecognizer 本身。
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?
我的问题是:如何在处理程序方法 (cellOneFingerDoubleTap) 中获取被点击的单元格?如何在处理程序方法中获取被点击单元格的索引?
Thanks!
谢谢!
回答by mattjgalloway
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:
如果您view
从在您的cellOneFingerDoubleTap:
方法中传递给您的手势识别器中获取 ,那么您将获得已被点击的单元格。就像是:
- (void)cellOneFingerDoubleTap:(UIGestureRecognizer*)recognizer {
UITableViewCell *cell = (UITableViewCell*)recognizer.view;
}
[I'm just assuming by a "cell" you mean a UITableViewCell
by the way]
[UITableViewCell
顺便说一下,我只是假设“单元格”是指您的意思]
回答by Conrad Shultz
UIGestureRecognizer has a
UIGestureRecognizer 有一个
-(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;
由于您将识别器附加到 UITableViewCell,因此将识别器的视图传递给 UITableView;
-indexPathForCell: