Search code examples
iosswiftxcodeseguexib

How to segue from a cell in xib to a VC in Storyboard


I'm creating an app with three table views (each in a different VC). I added everything to the main.storyboard, but I have to make one cell custom in a XIB file with three Labels. So my question is how can I add a segue from the cell in the XIB file to the ViewController in the Storyboard.

I saw that here's an answer but I don't understand this : Segue from Storyboard to XIB


Solution

  • If you know the position of your customCell then you can easily achieve this programmatically by calling tableView delegate following ways: Suppose your custom cell is in the last index then:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard indexPath.row == models.count - 1 else {
            //behavior of other cell
            return
        }
        navigateToTestViewController()
    }
    

    But if you have a button in a customCell then you have to use delegate or reactive approach to send delegate to parentController to perform navigation.

    Update: If you only need to make cell of type customCell navigable then you can do following way:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        guard cell is CustomCellTableViewCell else { return }
        navigateToTestViewController()
    }