Long gesture of clicking on a table cell

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

Long press on UITableView

The problem with this is that if I click on a valid cell for a long time, the cell highlights blue and the long gesture does not work (he thinks it's just a simple tap). However, if I start a long gesture of clicking on an invalid cell, then move my finger to the actual cell and then release it, it works fine.

+7
source share
4 answers

It is possible to disable the selection in IB or programmatically

[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
-2
source

There is probably a better answer, but here is one way to do this:

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

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

Then process it using a procedure 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 have not tried, but I think that you could keep the line in the selection display, forcing the gesture recognizers in the table view to wait until a long press is obtained.

+23
source

I ran into 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 strange, but important, to initialize with a goal for yourself, and again add gestureRecognizer to yourself and call the onSelfLongpressDetected method.

+2
source

I had a problem with this. At first I tried to add a long press gesture to the UIView inside the cell, and that didn't work. The solution was to add a gesture to the cell itself, as was said earlier in Fabio's answer.

Adding a solution to a quick squeak:

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

I used the code above inside the UITableViewDataSource cellForRowAtIndexPath method.

0
source

All Articles