Search code examples
iosswiftuitableviewuiviewcontrollersegue

Segue from Static Cells to Dynamic TableViewController


I have a TableViewController that has static cells inside groups. I want to perform segue from some of the static cells, but also somehow know which cell triggered the segue in the segued TableViewController.

I ctrl + dragged from each cell to the destination TVC, added segue identifiers and created a class for TVC.

I this approach, but the segue doesn't work. It doesn't print XOX either

class MainTableViewController {

  viewDidLoad() {
     tableView.delegate = self
     tableView.dataSource = self
  }

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      print("XOX")
      if segue.identifier == "MainToA" {        
        if let destination = segue.destinationViewController as? DetailTableViewController {
                destination.page = "A"
        }
      }

     if segue.identifier == "MainToB" {        
       if let destination = segue.destinationViewController as? DetailTableViewController {
               destination.page = "B"
       }
     }
  }

class DetailTableViewController {

    var page = String()

    viewDidLoad() {
         print(page)
    }
}

Am I missing out something or completely out of track? Why doesn't the segue work?


Solution

  • I tried and the segues work correctly with connection to individual UITableViewCell

    You can see in the image the triggered segue part

    Please make sure you have connected the segue from the cell instead of any subview inside it.

    Edit

    From the chat it appears you have a UITapGesture which takes up the touch of the view. To avoid this i would suggest to removeTapGesture and use tableView didSelectRow methods to detect touches on view