> 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]: