swiftuitableviewseguepushviewcontrollerdidselectrowatindexpath

did select method not segueing to view controller


My swift code in the did select method should segue to view controller class full screen. When I press the tableview cell nothing happens. Mainly focus on the did select method that is the code that should segue to another view controller.

var itemName : [NSManagedObject] = []
override func viewDidLoad() {
    super.viewDidLoad()

    theField.dataSource = self
    theField.delegate = self

    theField.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
}
var txtField = UITextField()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let title = itemName[indexPath.row]
    let cell = theField.dequeueReusableCell(withIdentifier: "MyCell", for : indexPath)

    cell.selectionStyle = .default
    let attr1 = title.value(forKey: "name") as? String



    let text = [attr1].flatMap { $0 }.reduce("", +)
    cell.textLabel?.text = "\(text)"

    cell.textLabel?.textAlignment = .center

    cell.layoutMargins = UIEdgeInsets.zero




    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero


    return cell

}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let cell = theField.cellForRow(at: indexPath)

    let vc = fullScreen()
    vc.tim = (cell?.textLabel!.text)!
    navigationController?.pushViewController(vc, animated: true)

  }

  class fullScreen : UIViewController{

var tim = ""
}

Solution

  • This might be because your navigationController is nil.

    try this instead present(vc, animated: true, completion: nil)