ios - UITextField 阻止 UITableViewCell 内的滚动

标签 ios objective-c uitableview uitextfield

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

相关文章:

ios - 如何以编程方式让 UIWebView 暂停/播放视频内容

ios - 不滚动刷新 UITableview

ios - UITableView - 将cornerRadius设置为图层不适用于顶角

iphone - 不明白崩溃日志?

ios - 更改委托(delegate)方法的参数类型

ios - 通过 UIScrollView 将某些触摸传递给底层 View

objective-c - iOS 6.0 Quicklook QLPreviewController 错误 : "Cannot find preview item for loaded proxy"

ios - UITableView reloadData 自动调用resignFirstResponder

ios - 如何自定义tableView编辑模式ios

ios - 如何在 Xcode 中设置 UITableView 部分标题渐变(和透明)背景?