【问题标题】:UITextField inside UITableViewCell swipe to delete issueUITableViewCell 内的 UITextField 滑动删除问题
【发布时间】:2014-11-23 12:59:47
【问题描述】:

从 iOS 8 开始,我在自定义 UITableViewCell 上遇到了滑动删除手势的问题。

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

这似乎是 iOS 8 中的一个问题,我在 iOS 7 中也有同样的代码。

如何保持 UITextField 可编辑和滑动删除手势同时工作?

【问题讨论】:

  • 经过一些测试,我可以确认这确实是 iOS 8 的问题。在 iOS 7 上不会出现此问题。
  • 如果你愿意,我找到了解决方法。
  • 那太好了!
  • 好的,我回家后会在回答中发布

标签: ios uitableview uitextfield ios8 swipe


【解决方案1】:

以下内容对我有用:

    self.tableView.panGestureRecognizer.delaysTouchesBegan = YES;

【讨论】:

    【解决方案2】:

    我在 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
    

    【讨论】:

    • 感谢分享。不过,我已将赏金授予另一个答案,因为它实现起来要简单得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多