2017-05-24 2 views
0

swift3의 스토리 보드를 통해 uitableview를 길게 누르는 중입니다. 스토리 보드에 하나의 프로토 타입 셀 세트 만 있습니다. 그러나 문제는 첫 번째 셀에서만 긴 누름이 감지되고 있다는 것입니다. 세포의 나머지 부분은 길게 눌러 제스처를 듣지 않습니다. 콘솔에 표시된테이블 뷰 셀에서 UILongPressGestureRecognizer를 사용하는 중

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 10 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     let row = indexPath.row 
     cell.textLabel?.text = "Label" 

     return cell 
} 

    @IBAction func longPress(_ guesture: UILongPressGestureRecognizer) { 

     if guesture.state == UIGestureRecognizerState.began { 
      print("Long Press") 
     } 
    } 

경고는 다음과 같습니다이 허용되지 않았다, 지금 적용됩니다

한 번에

. iOS 9.0부터는로드 된 첫 번째보기에 배치됩니다.

+0

제스처를 첨부 한보기는 무엇입니까? –

+0

uitableview cell –

+1

전체 tabelview에 longpressgesture를 추가하십시오. –

답변

1

당신은

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     let row = indexPath.row 
     cell.textLabel?.text = "Label" 

     let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(HomeViewController.longPress(_:))) 
     cell?.addGestureRecognizer(longPressRecognizer) 
     return cell 
} 



func longPress(_ guesture: UILongPressGestureRecognizer) { 

     if guesture.state == UIGestureRecognizerState.began { 
      print("Long Press") 
     } 
    } 
+0

왜 투표? – KKRocks

+0

모든 셀에 제스처를 첨부하지 마십시오. 하나의 gestureRecongnizer를 만들어 tableview에 저장하는 것이 좋습니다. https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/IncorporatingGestureSupport/IncorporatingGestureSupport.html "UICollectionView 클래스는 UIScrollView의 자손이므로 제스처 인식기를 콜렉션 뷰에 연결하는 가능성이 낮습니다 추적해야하는 다른 제스처를 방해합니다. " –

1

가있는 tableview에 제스처를 연결 cellForRowAtIndexPath의 모든 셀에 대한 제스처를 추가해야하고, 동작이 그림을 트리거 될 때 indexPath가 선택되었다.

override func viewDidLoad() { 
    super.viewDidLoad() 
     let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(_:))) 
    tableView?.addGestureRecognizer(longPressRecognizer) 
} 

func longPress(_ guesture: UILongPressGestureRecognizer) { 

    if guesture.state == UIGestureRecognizerState.began { 
     let point = guesture.location(in: tableView) 
     let indexPath = tableView.indexPathForRow(at: point); 
     print("Long Press \(String(describing: indexPath))") 
    } 
} 

있는 tableview이있는 ScrollView의 일종이기 때문에가있는 tableview 자신과 서브 뷰의되지 않은에 제스처를 부착하는 것이 가장 좋습니다. 이렇게하면 추적해야하는 다른 제스처를 방해 할 가능성이 줄어 듭니다.