0

In an iOS App, I have a UITableView (NOT part of a UITableViewController) on which I want to detect long press on custom UITableViewCells with the following method:

- (void)viewDidLoad
{
    myTableView.delegate=self;
    myTableView.dataSource=self;

    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCaptured:)];
    longTap.minimumPressDuration=0.5f;
    longTap.delegate=self;
    [myTableView addGestureRecognizer:longTap];
    [super viewDidLoad];
}

-(void)longTapGestureCaptured:(UILongPressGestureRecognizer *)gesture
{
    NSLog(@"Long tap"); // never called
}

However the longTapGestureCaptured is never called when I longpress. How can this be resolved ?

Naresh
  • 16,698
  • 6
  • 112
  • 113
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • 1
    Try adding the gesture recognizer to each cell. – koen Jun 01 '18 at 18:37
  • 1
    did you try adding it to the cell ? – Gihan Jun 01 '18 at 18:52
  • Yes I just tried, and while the first run indeed calls the event, all the subsequent runs (event after rebuild) do not call it at all. – Laurent Crivello Jun 01 '18 at 19:02
  • *"the first run indeed calls the event, all the subsequent runs"* ... Not clear what you mean by that... Do you mean if you run the app from Xcode, the gesture works, but then you quit the app and run it again and it doesn't work? Or do you mean you somehow navigate to the view controller, gesture works, you navigate away and then back and *then* it doesn't work? – DonMag Jun 01 '18 at 19:42
  • I run the app from XCode / simulator and longpress, then event will be called. Then I longpress again and event not called anymore. Stop, rebuild and rerun, longpress, and event still not called. – Laurent Crivello Jun 01 '18 at 19:50

2 Answers2

1

I tried your code. it's 100% working for me. But small change in your code is...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Create cell here
    UITableViewCell *cell;
    cell= (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    //Add gesture to cell here
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCaptured:)];
    longTap.minimumPressDuration=1.5f;
    longTap.delegate=self;
    [cell addGestureRecognizer:longTap];

    cell.textLabel.text = @"Name";//Send your data here

    return cell;

}

-(void)longTapGestureCaptured:(UILongPressGestureRecognizer *)gesture
{
    NSLog(@"Long tap"); // never called
}
Naresh
  • 16,698
  • 6
  • 112
  • 113
0

You add longPress even for UITableview. But when table have cells, cell of table will over on UITableView, so you can not longPress on TableView. Reslove for you is add longPress even on each Cell of Table. By add code add longPress even for each cell in function cellAtIndex. Good luck.

Thuan Nguyen
  • 244
  • 1
  • 14