UITextField里面的UITableViewCell滑动删除问题

时间:2014-09-29 07:36:52

标签: ios uitableview uitextfield ios8 swipe

自iOS 8起,我在自定义UITableViewCell上滑动删除手势时遇到问题。

问题似乎来自UITableViewCell的contentView内的UITextField。

这似乎是iOS 8中的问题,我在iOS 7中使用相同的代码。

如何保持UITextField的可编辑性以及滑动删除手势同时工作?

2 个答案:

答案 0 :(得分:6)

以下对我有用:

    self.tableView.panGestureRecognizer.delaysTouchesBegan = YES;

答案 1 :(得分:1)

我在iOS 8中找到了解决我的问题的方法

UITextField的子类并在UITextField的顶部添加一个视图,然后添加一个UIGestureRecognizer,用于单击"掩码"图。

#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