UITextField inside UITableViewCell swipe to delete

2019-06-27 05:07发布

问题:

Since iOS 8 I encounter an issue with the swipe to delete gesture on custom UITableViewCell.

The problem seems come from UITextField inside the contentView of UITableViewCell.

It seems to be a problem in iOS 8, I have the same code working fine in iOS 7.

How can I keep the UITextField editable and the swipe to delete gesture working at the same time?

回答1:

The following worked for me:

    self.tableView.panGestureRecognizer.delaysTouchesBegan = YES;


回答2:

I found a workaround for my issue in iOS 8

Subclass UITextField and add a view on top of the UITextField then add a UIGestureRecognizer for single tap on the "mask" view.

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@interface OMTextField : UITextField
@property (nonatomic,retain) NSNumber*canBecomeFirstResponderFlag;
@end

@implementation OMTextField

-(id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {

        _canBecomeFirstResponderFlag = @0;

        UIView*mask = [[UIView alloc] init];
        mask.translatesAutoresizingMaskIntoConstraints = NO;

        NSLayoutConstraint *maskT = [NSLayoutConstraint constraintWithItem:mask attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
        NSLayoutConstraint *maskB = [NSLayoutConstraint constraintWithItem:mask attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
        NSLayoutConstraint *maskL = [NSLayoutConstraint constraintWithItem:mask attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
        NSLayoutConstraint *maskR = [NSLayoutConstraint constraintWithItem:mask attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];

        [self addSubview:mask];
        [self addConstraints:@[maskT,maskB,maskL,maskR]];

        UITapGestureRecognizer *singleFingerTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(handleSingleTap:)];
        [mask addGestureRecognizer:singleFingerTap];

    }
  }
  return self;
}

-(BOOL)canBecomeFirstResponder{

  BOOL canBecomeFirstResponder;

  if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {

    canBecomeFirstResponder = [_canBecomeFirstResponderFlag boolValue];

    _canBecomeFirstResponderFlag = @0;

  }
  else{
    canBecomeFirstResponder = [self.delegate textFieldShouldBeginEditing:self];
  }

  return canBecomeFirstResponder;
}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {

  _canBecomeFirstResponderFlag = @1;

  BOOL souldBecomeFirstResponder = [self.delegate textFieldShouldBeginEditing:self];

  if (souldBecomeFirstResponder) {
    [self becomeFirstResponder];
  }

}

@end