OGeek|极客世界-中国程序员成长平台

标题: ios - 自定义单元格中的链接 UITextView 注册点击 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:13
标题: ios - 自定义单元格中的链接 UITextView 注册点击

我有一个自定义单元格,其中包含一个名为 StatusText 的 UITextView。我有一个扩展,可以在 textView 中找到所有 @ 符号并将其转换为链接。我希望能够单击该用户名链接并转到配置文件 View Controller 。

下面是CustomCell的代码:

import UIKit

class profileCell: UITableViewCell, UITextViewDelegate {
    @IBOutlet weak var UserImage: UIImageView!
    @IBOutlet weak var UserName: UILabel!
    @IBOutlet weak var Time: UILabel!
    @IBOutlet weak var Likes: UILabel!

    @IBOutlet weak var Dislikes: UILabel!

    @IBOutlet weak var StatusText: UITextView!

    @IBOutlet weak var LikeButton: UIButton!

    @IBOutlet weak var DislikeButton: UIButton!

      override func awakeFromNib() {
          StatusText.delegate = self
      }

func StatusText(StatusText: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    /* perform your own custom actions here */
    print("firing this right now")

    return true // return true if you still want UIAlertController to pop up
  }
}



Best Answer-推荐答案


因此,我没有将委托(delegate)设置为 customCell,而是将 viewController 设置为委托(delegate),并按照@kgaleman 所说的进行了如下操作,然后它被触发了。

func textView(_ textView: UITextView, shouldInteractWithURL URL: NSURL,   inRange characterRange: NSRange) -> Bool {
    /* perform your own custom actions here */

    print(URL)

    return true // return true if you still want UIAlertController to pop up

}

关于ios - 自定义单元格中的链接 UITextView 注册点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40924093/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4