long press gesture on table view cell

25,501

Solution 1

Maybe disabling selection in IB or programatically

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Solution 2

There is probably a better answer out there, but here is one way to do it:

First create a long press gesture recognizer on the table view itself.

UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
[self.tableView addGestureRecognizer:longPressRecognizer];

Then, handle it with a routine that can find the selected row:

-(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
    }
}
}

I haven't tried it, but I think you could keep the row from showing selection by making the gesture recognizers on the table view wait for the long press to fail.

Solution 3

I came across the same problem and found a good solution. (at least on iOS 7)

Add this UILongPressGestureRecognizer to the cell itself.

self.longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onSelfLongpressDetected:)];
[self addGestureRecognizer:self.longPressGesture];

Its weird but important to init with the target to self, AND also add the gestureRecognizer again to self and the method onSelfLongpressDetectedgets called.

Solution 4

I had a problem close to this. First I tried to add a long press gesture to an UIView inside a selectable cell and it didn't work. The solution was to add the gesture to the cell itself, like it was said before in Fabio's answer.

Adding the solution in swift bellow:

let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPress(_:)))
longPress.minimumPressDuration = 1.0
cell.addGestureRecognizer(longPress)

I used the code above inside the UITableViewDataSource method cellForRowAtIndexPath.

Share:
25,501
user1120008
Author by

user1120008

Updated on August 05, 2020

Comments

  • user1120008
    user1120008 over 3 years

    I want two interactions on a table view cell: normal tap and long press. I used the answer to the following to help me get started:

    Long press on UITableView

    The problem with that is if I do a long press on a valid cell, the cell will highlight blue, and the long press gesture does not fire (it thinks its just a simple tap). However, if I start the long press gesture on a non-valid cell, then slide my finger over to a valid cell then release, it works just fine.

  • user1120008
    user1120008 about 12 years
    Does this simply just disable the blue highlighting?
  • Teofilo Israel Vizcaino Rodrig
    Teofilo Israel Vizcaino Rodrig about 12 years
    My theory is that your long press gesture does not fire because the cell is already highlighted. Please, test my proposal, you will not lose anything.
  • ACAkgul
    ACAkgul over 5 years
    in onLongPress method you should use self.tableView instead of creating a new tableView instance. Also use self.tableView instead of self.view
  • ekashking
    ekashking about 3 years
    this is not a solution. I mean it works but we talking about clicking on a specific UITextView within the cell, and no like anywhere within the cell.