Long Press on Uitableview

Long press on UITableView

First add the long press gesture recognizer to the table view:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
[lpgr release];

Then in the gesture handler:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];

NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
} else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"long press on table view at row %ld", indexPath.row);
} else {
NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);
}
}

You have to be careful with this so that it doesn't interfere with the user's normal tapping of the cell and also note that handleLongPress may fire multiple times (this will be due to the gesture recognizer state changes).

How to make tableViewCell handle both tap and longPress?

Don't add the UILongPressGestureRecognizer to Cell. Add it to UITableView in viewDidLoad

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(sender:)))
tableView.addGestureRecognizer(longPress)

Get the touched cell index by

@objc private func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
let touchPoint = sender.location(in: tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
// your code here, get the row for the indexPath or do whatever you want
}
}
}

Instead of UITapGestureRecognizer use didSelectRowAtIndexPath is a better way

Long press gesture on UITableView

Change this:

let touchPoint = longPressGestureRecognizer.location(in: self.view)

To this:

let touchPoint = longPressGestureRecognizer.location(in: self.tableView)

You're looking for a gesture inside your UITableView not your UIView.

How to create a long press button in TableView Cell to segue

I found an easy yet unorthodox solution but works like a charm:

Place a second button into your TableView Cell, connect your segue, and set it to hidden. Then create your IBOutlet for it, which I named SegueButton:

@IBOutlet weak var LongPressButton: UIButton!
@IBOutlet weak var SegueButton: UIButton!

Now add to your awakeFromNib:

override func awakeFromNib() {
addLongPressGesture()
}

Lastly, add the long press button code:

@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Segue was Successful")
SegueButton.sendActions(for: .touchUpInside)
}
}

func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 0.75
self.LongPressButton.addGestureRecognizer(longPress)
}
}

How deselect UITableViewCell on long press?

Make a Global previousIndexPath Previous selected Index path

    //  Global variable
var previousIndexPath : IndexPath = IndexPath()

@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {

let p = longPressGesture.location(in: self.tblListView)
let indexPath = self.tblListView.indexPathForRow(at: p)

if indexPath == nil {
print("Long press on table view, not row.")
}
else if (longPressGesture.state == UIGestureRecognizer.State.began) {
print("Long press on row, at \(indexPath!.row)")

// Edited - Add this to reset cell when more than one selected
if !previousIndexPath.isEmpty {

// Reset the Cell
let cell = self.tblListView.cellForRow(at: previousIndexPath!) as! GroupDetailTableViewCell
cell.btnDeleteMember.isHidden = true

}

let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
cell.btnDeleteMember.isHidden = previousIndexPath == indexPath ?
true : false // This will make the Select and Deselect
previousIndexPath = indexPath
}
}

}

Long press action in didSelectRowAt

I found an answer!

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))

should be moved to viewDidLoad

Disable didSelectRowAtIndexPath on Long press gesture on Tableview Cell

@optimus comment helped me to solve this problem, this is a very genuine situation of disabling didSelectRowAtIndexPath when long press gesture is recognised on the tableview cell which has not been answered on the Internet

override func viewDidLoad() {

super.viewDidLoad()
setupLongPress()
setupTapPress()
}

extension GroupChatListingViewController : UIGestureRecognizerDelegate {

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

func setupLongPress() {

longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
self.tableView.addGestureRecognizer(longPressGesture)
}

func setupTapPress() {

singlePressGesture = UITapGestureRecognizer(target: self, action: #selector(tapPress))
singlePressGesture.delegate = self
singlePressGesture.cancelsTouchesInView = false
self.tableView.addGestureRecognizer(singlePressGesture)
}

func tapPress(gesture: UIGestureRecognizer) {

if gesture.state == .ended {
let touchPoint = gesture.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
// do your task on single tap
}
}
}

func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {

if longPressGestureRecognizer.state == UIGestureRecognizerState.began {

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


Related Topics



Leave a reply



Submit