我在 UITableViewCells 中有 UITextFields。 TextView 似乎阻止了滚动。当我将指尖放在 TextView 的范围内并尝试滚动整个表格时,它不会滚动。在 TextView 之外滚动效果很好。
我怎样才能阻止这种行为?
最佳答案
下面的代码有点 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
关于ios - UITextField 阻止 UITableViewCell 内的滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17151508/