我在UITableViewCells中有UITextFields。文本视图似乎阻止了滚动。当我将我的指尖放在文本视图的界限内并尝试滚动整个表格时,它将不会滚动。在文本视图之外滚动很好。
如何阻止此行为?
答案 0 :(得分:1)
似乎避免这种情况的唯一方法是继承UIScrollView并实现
(BOOL)touchesShouldCancelInContentView:(UIView *)view
然后,检测到视图是UITextField并返回YES,否则返回NO。
答案 1 :(得分:1)
下面的代码有点hacky但它对我有用,基本上只是将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