簡體   English   中英

在UITableView上長按手勢

[英]Long press gesture on UITableView

我一直在嘗試在uitableview上設置長按手勢識別器。 我已經能夠注冊它,但是似乎為我提供了有關該行的錯誤信息。 我的tableview可以正常單擊正常工作,它通過indexPath.row並且我能夠從與該行相關聯的people數組中獲取正確的記錄。 但是,在使用下面的代碼時,indexPath.row似乎不一致,並選擇上下行,然后滾動時長按它會選擇隨機記錄。

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)

更改此:

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

對此:

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

您正在UITableView而不是UIView尋找手勢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM