问题描述
我试图在本身可点击的 TableViewCell 中利用带有数据检测器类型的 UITextView 的优势.
I'm trying to have the advantage of the UITextView with Data Detector Types inside a TableViewCell that is itself clickable.
唯一的问题是,我需要 UITextView 的链接是可点击的,所以 userInteractionEnabled = YES,不幸的是,这将阻止任何触摸通过 UITableViewCell.
The only thing is, I need the UITextView's links to be clickable, so userInteractionEnabled = YES, unfortunately this will prevent any touch going through to the UITableViewCell.
当然不能编辑 UITextView,我也将它子类化,拒绝它成为第一响应者以避免选择其中的文本.
Of course the UITextView can't be edited, I also subclassed it refusing it to be first responder to avoid selecting text in it.
我唯一需要的是检测用户是否触摸 UITextView,检查它是否是一个链接,如果它正在打开链接,否则将触摸重定向到 UITableViewCell.
The only thing I need, is detecting that if a user touch the UITextView, check if it was a link, if it is then opening the link, otherwise redirect the touch on the UITableViewCell.
知道如何做到这一点吗?
any idea how can this be done ?
很像 Twitter 应用程序(我们可以点击行或链接...)
much like the twitter App (we can either click on the row, or on the link...)
推荐答案
我知道这个问题很久以前就有人问过了,但是对于某些应用程序来说,它仍然非常需要这种行为,因为它有一个带有 UIDataDetectors 的可点击单元.
I know that this question has been asked a while ago, but the behaviour is still very much needed for some app to have a clickable Cell with UIDataDetectors.
所以这是我为在 UITableView 中适应这种特定行为而编写的 UITextView 子类
So here's the UITextView subclass I made up to fit this particular behaviour in a UITableView
-(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;
}
您可以修改它以满足您的需要...
You can modify this to fit your needs...
希望对某人有所帮助.
这篇关于UITableViewCell 内的 UITextView - 如果不点击链接,则触摸通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!