在tableviewCell嵌套TextView,一般在評論功能里用的很多,例如微博。

WechatIMG78.jpeg
這里有個問題,里面一些藍色的地方需要響應(yīng)點擊,而點擊整個cell也需要響應(yīng)點擊事件,其中被TextView捕獲到之后,cell的點擊事件就不被響應(yīng)了。
這里我們需要點擊藍色部分響應(yīng)TextView點擊事件,點其他部分,響應(yīng)cell點擊事件
這里我們可以重寫一下TextView的點擊事件
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
var location = point
location.x -= self.textContainerInset.left
location.y -= self.textContainerInset.top
// find the character that's been tapped
let characterIndex = self.layoutManager.characterIndex(for: location, in: self.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if characterIndex < self.textStorage.length {
// if the character is a link, handle the tap as UITextView normally would
if (self.textStorage.attribute(NSLinkAttributeName, at: characterIndex, effectiveRange: nil) != nil) {
return self
}
}
// otherwise return nil so the tap goes on to the next receiver
return nil
}
解決了,意思就是如果點擊Textview沒有url的時候,將點擊事件傳到下一層,這樣的話,點擊非藍色部分,cell也可以響應(yīng)點擊事件了。