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

M Z*_*had 6 iphone objective-c uitableview ios

经过长时间但徒劳无功的搜索,我无法在我的tableview中检测到双击/触摸事件,实际上想要双击任何细节视图TableViewCell,实际上我甚至不知道如何在所有.

到目前为止这是我的代码......

viewDidLoad中

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

handleTapGesture方法是

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
  if (sender.state == UIGestureRecognizerStateRecognized) {
      flag = true;
  }
}
Run Code Online (Sandbox Code Playgroud)

最后触摸或点击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];
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我删除此标志条件,则只需单击即可调用新视图.我错在哪里或者还有其他方法可以做到这一点.

mer*_*hik 11

有一个简单的方法,不使用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
Run Code Online (Sandbox Code Playgroud)

它只是查看代码行.


Ali*_*aee 5

针对 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
}
Run Code Online (Sandbox Code Playgroud)