【问题标题】:UITextView inside UITableViewCell - touch go through if not clicking on a linkUITableViewCell 内的 UITextView - 如果不点击链接,则触摸通过
【发布时间】:2014-04-13 22:40:38
【问题描述】:

我正在尝试在本身可点击的 TableViewCell 中使用带有数据检测器类型的 UITextView 的优势。

唯一的事情是,我需要 UITextView 的链接是可点击的,所以 userInteractionEnabled = YES,不幸的是,这将阻止任何触摸通过 UITableViewCell。

当然 UITextView 不能被编辑,我也将它子类化,拒绝它成为第一响应者以避免在其中选择文本。

我唯一需要的是检测用户是否触摸 UITextView,检查它是否是一个链接,如果它正在打开链接,否则重定向 UITableViewCell 上的触摸。

知道怎么做吗?

很像 twitter 应用程序(我们可以点击行或链接...)

【问题讨论】:

标签: ios uitableview touch uitextview datadetectortypes


【解决方案1】:

我知道这个问题已经被问过一段时间了,但是某些应用程序仍然非常需要这种行为才能拥有带有 UIDataDetectors 的可点击单元。

这是我为适应 UITableView 中的这种特殊行为而编写的 UITextView 子类

-(id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (BOOL)canBecomeFirstResponder {
    return NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView *obj = self;

    do {
        obj = obj.superview;
    } while (![obj isKindOfClass:[UITableViewCell class]]);
    UITableViewCell *cell = (UITableViewCell*)obj;

    do {
        obj = obj.superview;
    } while (![obj isKindOfClass:[UITableView class]]);
    UITableView *tableView = (UITableView*)obj;

    NSIndexPath *indePath = [tableView indexPathForCell:cell];
    [[tableView delegate] tableView:tableView didSelectRowAtIndexPath:indePath];
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    return YES;
}

您可以修改它以满足您的需要...

希望对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多