如何检测 TabelCell 内的 UITextView 内的 link 是否已被单击,Swift

How to detect if a link, inside a UITextView inside a TabelCell has been clicked, Swift

我的目标是记录用户点击的所有 link。 link 嵌入在 table 单元格内的 UITextView 中,当用户按下 link 时,我希望它调用打印 url 的函数。

我看过类似的问题,例如 How to detect if a link, inside a UITextView has been clicked, SwiftHow to intercept click on link in UITextView? 人们建议使用函数 shouldInteractWithURL,但该函数从未被调用。

我想我可能在错误的地方设置了委托或函数,如果有人能告诉我应该如何设置就太好了。

以下是页面的缩略代码和部分截图:

class BSPdetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {


    func textView(textView: UITextView!, shouldInteractWithURL URL: NSURL!, inRange characterRange: NSRange) -> Bool {
        print("Link Selected!")
        return true

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell", for: indexPath as IndexPath) as! detailCell
        cell.label?.text = ""


        cell.detailLinks.delegate = self
        cell.detailLinks.isUserInteractionEnabled = true // default: true
        cell.detailLinks.isEditable = false // default: true
        cell.detailLinks.isSelectable = true // default: true
        cell.detailLinks.dataDetectorTypes = [.link]

        cell.detailTextLabel?.font = UIFont.fontAwesome(ofSize: 20)
        cell.detailTextLabel?.text = ""
        cell.detailTextLabel?.numberOfLines = 0;


        cell.detailLinks?.text = "links are here"
        var linkString = NSMutableAttributedString(string: "", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 22.0)])
        let video =  String.fontAwesomeIcon(code: "fa-video-camera")! + "  "
        let document = String.fontAwesomeIcon(code: "fa-file-text")! + "\n\n"

        for i in 0..<stratList.count {
            var stratURL = stratList[i].lowercased().replacingOccurrences(of: " ", with: "-")

            var docLink = "http://" + stratURL
            var videoLink = "http://" + stratURL

            //setting the url
            var attributedString = NSMutableAttributedString(string: video, attributes: [NSFontAttributeName: UIFont.fontAwesome(ofSize: 22)])
            attributedString.addAttribute(NSLinkAttributeName, value: docLink, range: NSRange(location: 0, length: 1))
            linkString.append(attributedString as NSAttributedString)

            attributedString = NSMutableAttributedString(string: document, attributes: [NSFontAttributeName: UIFont.fontAwesome(ofSize: 22)])
            attributedString.addAttribute(NSLinkAttributeName, value: videoLink, range: NSRange(location: 0, length: 1))
            linkString.append(attributedString as NSAttributedString)


            cell.label?.text = (cell.label?.text)! + "• " + stratList[i] + "\n\n"


            cell.detailLinks?.attributedText = linkString

            //tried tapgesture but it didn't work when I tapped on the links
            //cell.detailLinks.addGestureRecognizer(UITapGestureRecognizer(target: cell.detailLinks, action: #selector(cell.detailLinks.singleTap(tap:))))

            cell.backgroundColor = contentColors[rowNo]
            cell.label?.textColor = UIColor.black
        }


        return cell
    }
}

class detailCell: UITableViewCell
{

    @IBOutlet weak var detailLinks: UITextView!
    @IBOutlet weak var label: UILabel!

}

结果table:

UITextViewDelegate 方法 shouldInteractWithURL 应该写在 cellForRowAt 之外而不是里面。您的标准 UITextView 设置应如下所示,不要忘记委托和 dataDetectorTypes。

    cell.detailLinks.delegate = self
    detailLinks.isUserInteractionEnabled = true // default: true
    detailLinks.isEditable = false // default: true
    detailLinks.isSelectable = true // default: true
    detailLinks.dataDetectorTypes = [.link]

UITextViewDelegate方法shouldInteractWithURL:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    print("Link Selected!")
    return true
}