Get tapped cell in UITapGestureRecognizer handler

微笑、不失礼 提交于 2019-12-12 15:58:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!