我在 UITableViewCells 中有 UITextFields。文本视图似乎阻止了滚动。当我将指尖放在文本视图的范围内并尝试滚动整个表格时,它不会滚动。在文本视图之外滚动很好。
我怎样才能阻止这种行为?
我在 UITableViewCells 中有 UITextFields。文本视图似乎阻止了滚动。当我将指尖放在文本视图的范围内并尝试滚动整个表格时,它不会滚动。在文本视图之外滚动很好。
我怎样才能阻止这种行为?
下面的代码有点笨拙,但它对我有用,基本上只是将 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
似乎避免这种情况的唯一方法是将 UIScrollView 子类化并实现
(BOOL)touchesShouldCancelInContentView:(UIView *)view
然后,检测视图是 UITextField 并返回 YES,否则返回 NO。