Ios 如何在tableview中检测双击/触摸事件

Ios 如何在tableview中检测双击/触摸事件,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,经过漫长但徒劳的搜索后,我无法在我的tableview中检测到双击/触摸事件,实际上我想在任何TableViewCell上双击调用细节视图,而实际上我根本不知道如何做 这是我到目前为止的代码 在viewDidLoad中 handleTappsive方法是 - (void)handleTapGesture:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateRecognized) {

经过漫长但徒劳的搜索后,我无法在我的tableview中检测到双击/触摸事件,实际上我想在任何
TableViewCell
上双击调用细节视图,而实际上我根本不知道如何做

这是我到目前为止的代码

在viewDidLoad中

handleTappsive方法是

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
  if (sender.state == UIGestureRecognizerStateRecognized) {
      flag = true;
  }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (flag == true)
    {
        DetailInvoicing *detail = [[DetailInvoicing alloc] initWithNibName:@"DetailInvoicing" bundle:nil];
        detail.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        detail.customerName = [customerArray objectAtIndex:indexPath.row];
        [self presentViewController:detail animated:YES completion:nil];
    }
}
最后触摸或轻触tableview的单元格 委托方法是

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
  if (sender.state == UIGestureRecognizerStateRecognized) {
      flag = true;
  }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (flag == true)
    {
        DetailInvoicing *detail = [[DetailInvoicing alloc] initWithNibName:@"DetailInvoicing" bundle:nil];
        detail.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        detail.customerName = [customerArray objectAtIndex:indexPath.row];
        [self presentViewController:detail animated:YES completion:nil];
    }
}
如果我删除此标志条件,只需单触即可调用新视图。
我哪里错了,或者还有其他方法吗。

尝试这样做,在customtableview类中实现以下方法:-

 - (void)touchesEnded:(NSSet *)touches 
    withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
  // below numberofclick in integer type
   self.numberOfClicks = [aTouch tapCount];
 [super touchesEnded:touches withEvent:event];
 }
现在,在tableview委托方法(DidSelectRowatineXpath)中。然后检测numberOfclicks是2还是1,并相应地修改您的条件。下面是一行代码,您可以将其放入委托方法中,以检测按下的点击数

   if (myCell.numberOfClicks == 2) {
   NSLog(@"Double clicked");   }}

有一个简单的方法,不使用
UITapGestureRecognizer
也不实现自定义表视图类

@implementation MyTableViewController
NSTimeInterval lastClick;
NSIndexPath *lastIndexPath;

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSTimeInterval now = [[[NSDate alloc] init] timeIntervalSince1970];
    if ((now - lastClick < 0.3) && [indexPath isEqual:lastIndexPath]) {
        // Double tap here
        NSLog(@"Double Tap!");
    }
    lastClick = now;
    lastIndexPath = indexPath;
}
@end
@实现MyTableViewController
NSTimeInterval lastClick;
nsindepath*lastIndexPath;
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
NSTimeInterval now=[[[NSDate alloc]init]TimeIntervalnces1970];
if((现在-lastClick<0.3)和&[indexPath相等:lastIndexPath]){
//双击这里
NSLog(@“双击!”);
}
lastClick=现在;
lastIndexPath=indepath;
}
@结束

它只是查看代码行。

针对Swift 3.1进行了更正

var lastClick: TimeInterval
var lastIndexPath: IndexPath? = nil

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let now: TimeInterval = Date().timeIntervalSince1970
    if (now - lastClick < 0.3) &&
        (lastIndexPath?.row == indexPath.row ) {
         print("Double Tap!")
    }
    lastClick = now
    lastIndexPath = indexPath
}
var lastClick:时间间隔
var lastIndexPath:IndexPath?=无
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
现在让我们来看一下:TimeInterval=Date()。timeIntervalSince1970
如果(现在-上次单击<0.3)&&
(lastIndexPath?.row==indexPath.row){
打印(“双击!”)
}
lastClick=现在
lastIndexPath=indepath
}

请替换
标志=true带有
标志=是
并将
flag==true
替换为just
flag
。您可以参考此链接,希望您的问题能够得到解决。感谢您为什么不在检测到双击的位置显示视图(在Handletapsignature中)?@nhgrif它可以工作,但不是特别在双击时…当我双击时,什么都没有发生,然后它在第三次点击时调用了新视图,有时在第四次点击时调用了新视图…@Anna我必须将单元格值发送到新视图,这就是为什么使用这种方式。请解释最后两个变量lastClick和lastIndexPath如何声明这两个变量以及在哪里?