2017-01-26 21 views
0

私は、私のuitableviewで長いプレスジェスチャー認識装置をセットアップしようとしています。私はそれを登録することができましたが、それは私に関連する行の情報が間違っているようです。私のテーブルビューは通常のクリックで正常に動作し、indexPath.rowが渡され、その行に関連付けられたpeopleの配列から正しいレコードを取得できます。しかし、以下のコードを使用すると、indexPath.rowは矛盾しているように見え、上下の行を選択し、スクロールすると長押しでランダムなレコードを選択します。UITableViewで長押しのジェスチャー

func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) { 

    if longPressGestureRecognizer.state == UIGestureRecognizerState.began { 

     let touchPoint = longPressGestureRecognizer.location(in: self.view) 
     if let indexPath = tableView.indexPathForRow(at: touchPoint) { 
      let person = people[indexPath.row] 
      print("\(person.name)") 
      ///////works but erratic responses////////// 
     } 
    } 
} 

//// in view did load i have this 


    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(PeopleVC.longPress(_:))) 
    longPressGesture.minimumPressDuration = 1.0 // 1 second press 
    longPressGesture.delegate = self 
    self.tableView.addGestureRecognizer(longPressGesture) 
+0

コントローラのメインビュー(コントローラがUITableViewControllerの場合は問題を解決しません): 'longPressGestureRecognizer.location(in:self.tableView)'と 'longPressGestureRecognizer.location(in:self.view) ' – EPerrin95

+0

あなたはこのコードをuitableviewcellに入れてみて、そのために代理人を作ってください。 –

+0

ありがとう、完全に意味する – user1881482

答えて

2

変更この:これに

let touchPoint = longPressGestureRecognizer.location(in: self.view) 

let touchPoint = longPressGestureRecognizer.location(in: self.tableView) 

あなたのUITableViewないあなたUIView内部のジェスチャーを探しています。

+2

作品。ありがとうございました。 – user1881482

関連する問題