私は、私の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)
コントローラのメインビュー(コントローラがUITableViewControllerの場合は問題を解決しません): 'longPressGestureRecognizer.location(in:self.tableView)'と 'longPressGestureRecognizer.location(in:self.view) ' – EPerrin95
あなたはこのコードをuitableviewcellに入れてみて、そのために代理人を作ってください。 –
ありがとう、完全に意味する – user1881482