2015-06-15 13 views
11

Ho una tabella che ha un riconoscitore di gesture a pressione lunga che esegue il codice a seconda della riga della tabella selezionata.Come selezionare una riga della tabella durante una pressione prolungata in Swift

Il problema che sto avendo è che attualmente devo toccare la riga che voglio poi fare la lunga pressione.

Come posso fare in modo che la tabella selezioni la riga che sto premendo a lungo senza dover toccare per selezionarla prima?

risposta

1

Per evitare questo, è possibile aggiungere il UILongPressGestureRecognizer all'interno del cellForRowAtIndexPath invece di didSelectRowAtIndexPath

21

Il seguente codice funziona bene per me:

Aggiungi una pressione prolungata gesto riconoscitore in viewDidLoad:

// tapRecognizer, placed in viewDidLoad 
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:") 
self.view.addGestureRecognizer(longPressRecognizer) 

Quindi il metodo richiamato dalla pressione lunga appare così:

//Called, when long press occurred 
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) { 

    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began { 

     let touchPoint = longPressGestureRecognizer.locationInView(self.view) 
     if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) { 

      // your code here, get the row for the indexPath or do whatever you want 
    } 
} 
+0

è già raggiunto la funzionalità e di fronte al problema con un tap in più. –

+0

Non sono sicuro, se ho capito bene il problema. Quello che posso dire è che con il mio codice faccio una lunga pressione e ottengo il parametro IndexPath della riga su cui ho fatto la lunga pressione. Non è necessario alcun tocco aggiuntivo. – user3687284

+3

'let touchPoint = longPressGestureRecognizer.locationInView (self.tableView)' è corretto invece di 'let touchPoint = longPressGestureRecognizer.locationInView (self.view)'. Altrimenti, non si tiene conto del fatto che 'self.tableView.frame.origin' non deve essere uguale a' CGPointZero' –

1

Usando IBAction si può fare (per un CollectionView):

@IBAction func handleLongPress(sender: AnyObject) { 

    if sender.state == UIGestureRecognizerState.Began 
    { 
     let position = sender.locationInView(sender.view) 

     if let indexPath : NSIndexPath = ((sender.view as! UICollectionView).indexPathForItemAtPoint(position))!{ 
      print("You holding cell #\(indexPath.item)!") 
     } 
    } 
} 

Ricordati di collegamento con il vostro lungo Press Gesture Recognizer.

14

Swift 3 funzione:

func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) { 

    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began { 

     let touchPoint = longPressGestureRecognizer.locationInView(self.view) 
     if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) { 

      // your code here, get the row for the indexPath or do whatever you want 
    } 
} 

viewDidLoad:

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

Più: https://github.com/apple/swift-evolution/blob/e4328889a9643100177aef19f6f428855c5d0cf2/proposals/0046-first-label.md

+0

Non è per Swift 3.0 come hai menzionato. sta ancora puntando a swift <3.0 – Sahil

+0

Ha funzionato perfettamente in Swift 3 per me. –

0
let longPressGesture = UILongPressGestureRecognizer(target: self, action: (#selector(YourCustomeTableCell.longTap))) 
self.addGestureRecognizer(longPressGesture) 

func longTap(){ 
    print("Long tap") 
} 
6

Per Verision 3 su rapida

func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) { 
    if longPressGestureRecognizer.state == UIGestureRecognizerState.began { 
     let touchPoint = longPressGestureRecognizer.location(in: self.view) 
     if let indexPath = notificationTabelView.indexPathForRow(at: touchPoint) { 
      print("indexPath=\(indexPath)") 
      // your code here, get the row for the indexPath or do whatever you want 
     } 
    } 
} 

In Your viewDidLoad funzione

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(EmployeeNotificationViewController.longPress(_:))) 
    longPressGesture.minimumPressDuration = 1.0 // 1 second press 
    longPressGesture.delegate = self as? UIGestureRecognizerDelegate 
    self.notificationTabelView.addGestureRecognizer(longPressGesture) 
1

Swift 4

override func viewDidLoad() { 
    super.viewDidLoad() 
    setupLongPressGesture() 
} 

func setupLongPressGesture() { 
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress)) 
    longPressGesture.minimumPressDuration = 1.0 // 1 second press 
    longPressGesture.delegate = self 
    self.tblMessage.addGestureRecognizer(longPressGesture) 
} 

@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){ 
    if gestureRecognizer.state == .ended { 
     let touchPoint = gestureRecognizer.location(in: self.tblMessage) 
     if let indexPath = tblMessage.indexPathForRow(at: touchPoint) { 

     } 
    } 
} 
Problemi correlati