【问题标题】:How to detect double tap / touch event in tableview如何在表格视图中检测双击/触摸事件
【发布时间】:2014-04-20 20:31:20
【问题描述】:

经过漫长但徒劳的搜索,我无法在我的 tableview 中检测到双击/触摸事件,实际上想在任何 TableViewCell 上调用双击时的详细视图,实际上我什至没有完全知道该怎么做。

这是我目前的代码……

viewDidLoad

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.myTable addGestureRecognizer:tapGesture];

handleTapGesture方法是

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
  if (sender.state == UIGestureRecognizerStateRecognized) {
      flag = true;
  }
}

最后触摸或点击 tableview 的单元格 委托方法是

- (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];
    }
}

如果我删除此标志条件,只需单击一下即可调用新视图。 我在哪里错了,或者还有其他方法吗。

【问题讨论】:

  • 请将flag = true; 替换为flag = YES; 并将flag == true 替换为flag
  • 你可以参考这个链接,希望你的问题能得到解决。 stackoverflow.com/questions/1031254/…谢谢
  • 为什么不在您检测到双击的位置显示视图(在 handleTapGesture 中)?
  • @nhgrif 它可以工作,但不是特别在双击时……当我双击时什么也没发生,然后它在第三次和第四次点击时调用了新视图……
  • @Anna 我必须将单元格值发送到新视图,这就是为什么使用这种方式

标签: ios iphone objective-c uitableview


【解决方案1】:

有一个简单的方法,不使用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

只是查看代码行。

【讨论】:

    【解决方案2】:

    针对 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
    }
    

    【讨论】:

    • 你能解释一下最后两个变量 lastClick 和 lastIndexPath 你是如何声明这两个变量的吗?
    【解决方案3】:

    尝试这样,在您的 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 委托方法 (didSelectRowAtIndexPath) 中。然后检测 numberOfclicks 是 2 还是 1 并相应地修改您的条件。以下是您可以放入委托方法中以检测按下的点击次数的代码行

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      相关资源
      最近更新 更多