Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To select a table row during a long press in Swift

I have a table which has a long press gesture recogniser that runs code depending on what table row is selected.

The trouble I'm having is that I currently have to tap the row I want then do the long press.

How can I make the table select the row that I am long pressing without having to tap to select it first?

like image 765
LB79 Avatar asked Jun 15 '15 07:06

LB79


2 Answers

Swift 4 & 5

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 == .began {         let touchPoint = gestureRecognizer.location(in: self.tblMessage)         if let indexPath = tblMessage.indexPathForRow(at: touchPoint) {                      }     } } 

Swift 3

override func viewDidLoad() {     super.viewDidLoad()     setupLongPressGesture() }  func setupLongPressGesture() {     let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))     longPressGesture.minimumPressDuration = 1.0 // 1 second press     longPressGesture.delegate = self     self.tblMessage.addGestureRecognizer(longPressGesture) }  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         }     } } 

Objective - C

UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)]; [self.tableView addGestureRecognizer:longPressRecognizer];  -(void)onLongPress:(UILongPressGestureRecognizer*)pGesture {     if (pGesture.state == UIGestureRecognizerStateRecognized)     {     //Do something to tell the user!     }      if (pGesture.state == UIGestureRecognizerStateEnded)     {         UITableView* tableView = (UITableView*)self.view;         CGPoint touchPoint = [pGesture locationInView:self.view];         NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint];         if (row != nil) {         //Handle the long press on row         }     } } 
like image 121
PinkeshGjr Avatar answered Oct 19 '22 08:10

PinkeshGjr


The following code works fine for me:

Add a long press gesture recognizer in viewDidLoad:

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

Then the method invoke by the long press looks like this:

//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     } } 
like image 21
user3687284 Avatar answered Oct 19 '22 06:10

user3687284