【问题标题】:UITextField blocks scrolling within UITableViewCellUITextField 块在 UITableViewCell 内滚动
【发布时间】:2013-06-17 15:41:16
【问题描述】:

我在 UITableViewCells 中有 UITextFields。文本视图似乎阻止了滚动。当我将指尖放在文本视图的范围内并尝试滚动整个表格时,它不会滚动。在文本视图之外滚动很好。

如何阻止这种行为?

【问题讨论】:

    标签: ios objective-c uitableview uitextfield


    【解决方案1】:

    下面的代码有点笨拙,但它对我有用,基本上只是将 UITextView 上的 userInteractionEnabled 设置为 NO,然后在检测到用户点击它时设置为 YES。下面是带有 textview 的自定义单元格的代码,当用户从 UITextView 中启动滚动时,它滚动对我来说很好。这有效,因为滚动是平移手势而不是点击。

    #import "MyCell.h"
    
    @interface MyCell () <UITextFieldDelegate>
    
    @property (weak, nonatomic) IBOutlet UITextField *textField;
    
    @end
    
    @implementation MyCell
    
    - (void)awakeFromNib
    {
        self.textField.userInteractionEnabled = NO;
        self.textField.delegate = self;
    
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
        [self addGestureRecognizer:tapRecognizer];
    }
    
    #pragma mark - Private methods
    
    - (void)didTap:(UITapGestureRecognizer *)tapRecognizer
    {
        CGPoint location = [tapRecognizer locationInView:self];
    
        CGRect pointRect = CGRectMake(location.x, location.y, 1.0f, 1.0f);
        if (!CGRectIsNull(CGRectIntersection(pointRect, self.textField.frame))) {
            self.textField.userInteractionEnabled = YES;
            [self.textField becomeFirstResponder];
        } 
    }
    
    #pragma mark - UITextFieldDelegate methods
    
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        self.textField.userInteractionEnabled = NO;
    }
    
    @end
    

    【讨论】:

      【解决方案2】:

      似乎避免这种情况的唯一方法是继承 UIScrollView 并实现

      (BOOL)touchesShouldCancelInContentView:(UIView *)view
      

      然后,检测视图是 UITextField 并返回 YES,否则返回 NO。

      【讨论】:

      • 嘿@AntonioMG,我认为这通常是正确的方法,但是在处理表格视图时,传递给 touchesShouldCancelInContentView: 的 UIView 对象总是看起来是 UITableViewCellContentView 的实例,而不是子视图,所以不要以为你可以测试哪个视图被点击了
      • 我通过在笔尖中选择表格视图的“延迟内容触摸”解决了这个问题
      • 酷,我要试试,比我做的要容易得多
      猜你喜欢
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多