簡體   English   中英

UITextView在UITableViewCell中 - 如果不點擊鏈接,觸摸即可完成

[英]UITextView inside UITableViewCell - touch go through if not clicking on a link

我試圖在TableViewCell中使用帶有數據檢測器類型的UITextView,這本身就是可點擊的。

唯一的問題是,我需要UITextView的鏈接是可點擊的,所以userInteractionEnabled = YES,不幸的是,這將阻止任何觸摸進入UITableViewCell。

當然UITextView無法編輯,我還將其子類化,拒絕它作為第一響應者,以避免在其中選擇文本。

我唯一需要的是檢測到如果用戶觸摸UITextView,檢查它是否是鏈接,如果是然后打開鏈接,否則重定向UITableViewCell上的觸摸。

任何想法怎么能這樣做?

很像Twitter應用程序(我們可以點擊行,或鏈接...)

我知道這個問題已經被問過了一段時間,但是某些應用程序仍然非常需要一個具有UIDataDetectors的可點擊單元格的行為。

所以這里是UITextView子類,我在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;
}

您可以修改它以滿足您的需求......

希望它可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM