Search code examples
swiftuitableviewdelegatesuitextviewsegue

Trying to segue after tapping hashtag in UITextView from a UITableViewCell class


Using URL.scheme i'm getting the hashtag being tapped on in a UITextView inside a UITableViewCell class but i have two issues.

First how to segue from a cell class. I don't have perform segue function. Only found in the UITableView class.

Second how to send the hashtag name or the mention name to the new UIViewController. I can use delegate methods or send it through perform segue. But again how will i segue from the cell class.

The next code is written in the 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
}

Solution

  • There are several different ways of doing this, but I would probably use a custom delegate protocol. Define the protocol in your cell file as follows:

    protocol MyTableViewCellDelegate: class {
        func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String)
    }
    

    Add a property to your table view cell class:

    class MyTableViewCell: UITableViewCell {
        // make sure the property is `weak`
        weak var delegate: MyTableViewCellDelegate?
    }
    

    I'm assuming your table view data source is also the view controller from which you want to perform the segue. Make this view controller conform to the new protocol:

    extension MyViewController: MyTableViewCellDelegate {
        func myTableViewCell(_ cell: MyTableViewCell, shouldSelectHashTag tag: String) {
            performSegue(withIdentifier: "MyHashTagSegue", sender: tag)
        }
    }
    

    Assign your view controller as the delegate of the table view cell inside the cellForRowAtIndexPath data source method:

    let cell: MyTableViewCell = <dequeue the cell>
    cell.delegate = self
    

    Finally, don't forget to call the delegate method from your table view cell:

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        let tag = <get the hash tag> 
        delegate?.myTableViewCell(self, shouldSelectHashTag: tag)
    }