2017-01-26 6 views
0

필자는 uitableview에서 긴 프레스 제스처 인식기를 설정하려고했습니다. 등록 할 수있게되었지만 관련 행에 대해 잘못된 정보를 제공하는 것으로 보입니다. 내 tableview 정상적인 클릭 예상대로 작동합니다, 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