从 UITableViewCell class 中点击 UITextView 中的主题标签后尝试继续
Trying to segue after tapping hashtag in UITextView from a UITableViewCell class
使用 URL.scheme
我正在 UITableViewCell
class 中的 UITextView
中点击标签,但我有两个问题。
首先 如何从单元格 class 开始。我没有 perform
segue 功能。仅在 UITableView
class.
中找到
第二如何将标签名称或提及名称发送到新UIViewController
。我可以使用委托方法或通过 perform segue 发送它。但是我又将如何从单元格 class.
继续
接下来的代码写在UITableViewCell
class
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
let path = URL.absoluteString
switch URL.scheme! {
case "hash" :
let hash = path.removingPercentEncoding?.components(separatedBy: ":").last
print(hash!) // ---> Retriving tapped on hash name correctly
case "mention" :
let mention = path.removingPercentEncoding?.components(separatedBy: ":").last
print(mention!) // ---> Retriving tapped on mention name correctly
default:
print("Just a regular link \(path.removingPercentEncoding!)")
}
return true
}
有几种不同的方法可以做到这一点,但我可能会使用自定义委托协议。在您的单元格文件中定义协议如下:
protocol MyTableViewCellDelegate: class {
func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String)
}
将 属性 添加到您的 table 视图单元格 class:
class MyTableViewCell: UITableViewCell {
// make sure the property is `weak`
weak var delegate: MyTableViewCellDelegate?
}
我假设您的 table 视图数据源也是您要从中执行转场的视图控制器。使这个视图控制器符合新协议:
extension MyViewController: MyTableViewCellDelegate {
func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String) {
performSegue(withIdentifier: "MyHashTagSegue", sender: tag)
}
}
将您的视图控制器指定为 cellForRowAtIndexPath
数据源方法中 table 视图单元格的委托:
let cell: MyTableViewCell = <dequeue the cell>
cell.delegate = self
最后,不要忘记从 table 视图单元格调用委托方法:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
let tag = <get the hash tag>
delegate?.myTableViewCell(self, shouldSelectHashTag: tag)
}
使用 URL.scheme
我正在 UITableViewCell
class 中的 UITextView
中点击标签,但我有两个问题。
首先 如何从单元格 class 开始。我没有 perform
segue 功能。仅在 UITableView
class.
第二如何将标签名称或提及名称发送到新UIViewController
。我可以使用委托方法或通过 perform segue 发送它。但是我又将如何从单元格 class.
接下来的代码写在UITableViewCell
class
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
let path = URL.absoluteString
switch URL.scheme! {
case "hash" :
let hash = path.removingPercentEncoding?.components(separatedBy: ":").last
print(hash!) // ---> Retriving tapped on hash name correctly
case "mention" :
let mention = path.removingPercentEncoding?.components(separatedBy: ":").last
print(mention!) // ---> Retriving tapped on mention name correctly
default:
print("Just a regular link \(path.removingPercentEncoding!)")
}
return true
}
有几种不同的方法可以做到这一点,但我可能会使用自定义委托协议。在您的单元格文件中定义协议如下:
protocol MyTableViewCellDelegate: class {
func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String)
}
将 属性 添加到您的 table 视图单元格 class:
class MyTableViewCell: UITableViewCell {
// make sure the property is `weak`
weak var delegate: MyTableViewCellDelegate?
}
我假设您的 table 视图数据源也是您要从中执行转场的视图控制器。使这个视图控制器符合新协议:
extension MyViewController: MyTableViewCellDelegate {
func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String) {
performSegue(withIdentifier: "MyHashTagSegue", sender: tag)
}
}
将您的视图控制器指定为 cellForRowAtIndexPath
数据源方法中 table 视图单元格的委托:
let cell: MyTableViewCell = <dequeue the cell>
cell.delegate = self
最后,不要忘记从 table 视图单元格调用委托方法:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
let tag = <get the hash tag>
delegate?.myTableViewCell(self, shouldSelectHashTag: tag)
}