1
votes

Basically what I want to achieve is having a UITextField inside a custom UITableViewCell while this textfield should only triggers on long press. So if the user just taps once on the textfield, the cell should triggers its delegate method didSelectRow.

So far I added a long press on my textfield and removed all others gestures. So my textfield correctly triggers when I'm long pressing it. But the problem is that when I'm just tapping on the textfield, the cell doesn't get triggered and the didSelectRow method doesn't get called. If I tap outside of my textfield, the method is correctly called ofc.

Here is my code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.nameTextField.gestureRecognizers = nil;

    UILongPressGestureRecognizer *longPressTextField = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleCellTextFieldLongPress:)];
    [cell.nameTextField addGestureRecognizer:longPressTextField];

    return cell;
}

I tried different things with the UIGestureRecognizerDelegate but none seemed to work :(.

Any ideas appreciated ! Thx !

2

2 Answers

2
votes

You should be able to accomplish this by just having your activators on the cell itself. Because your UITextField is still accepting user input, it will be stopping the underlying cell from receiving the touch.

You could get around this by adding the UILongPressGestureRecognizer to the CustomCell object and setting:

cell.nameTextField.userInteractionEnabled = NO;
1
votes

Well I finally found a working solution.

I added a transparent view on top of my UITextField and I added my long press recognizer on this view. By this way the tap gesture goes "through" the transparent view and my cell get correctly selected :-)

Thx anyway !