Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 303 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To select a table row during a long press in Swift

#1
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?
Reply

#2
> **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
}
}
}
Reply

#3
> Swift 5

Declare this line in `viewDidLoad()` like this



override func viewDidLoad() {
super.viewDidLoad()
//do other stuff here

// long press listener for tableview
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
tableView.addGestureRecognizer(longPress)

}

Where `handleLongPress()` method is

@objc private func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
let alert = UIAlertController(title: "Alert", message: "Do you want to delete this item?", preferredStyle: .alert)

let action = UIAlertAction(title: "Yes", style: .default) { (action) in
// do your functionality

alert.dismiss(animated: true, completion: nil)

}
let actionDelete = UIAlertAction(title: "No", style: .default) { (action) in
// do your functionality
alert.dismiss(animated: true, completion: nil)

}

alert.addAction(action)
alert.addAction(actionDelete)
self.present(alert, animated: true, completion: nil)
// your code here, get the row for the indexPath or do whatever you want
}
}
}

[![enter image description here][1]][1]


[![enter image description here][2]][2]


[1]:

[2]:
Reply

#4
***Swift 4***

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
self.view.addGestureRecognizer(longPressRecognizer)

// MARK: Actions

@objc func longPressed(sender: UILongPressGestureRecognizer) {

if sender.state == UIGestureRecognizerState.began {

let touchPoint = sender.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {

print("Long pressed row: \(indexPath.row)")
}
}
}
Reply

#5
For Verision 3 of swift

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` function

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)
Reply

#6
Swift 3 function:

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)


More:

[To see links please register here]

Reply

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

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

Reply

#8
Using IBAction you can do (for a 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)!")
}
}
}

Remember to link with your Long Press Gesture Recognizer.
Reply

#9
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
}
}
Reply

#10
To avoid this, you can add the `UILongPressGestureRecognizer` inside the `cellForRowAtIndexPath` instead of `didSelectRowAtIndexPath`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through