swiftuitableviewindexingseguerootviewcontroller

perform a root viewcontroller segue programmatically on a tableviewcell in swift


The code below creates a tableviewcell and places a object in it. The code to place data inside the tableviewcell is not present. All I want to do is when the user clicks on the tableviewcell it to be transfers to a basic view controller class. No need to show how to go back.

   var itemName : [NSManagedObject] = []
var theField = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()


    theField.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
}
 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

}

 @objc func moveRight() {

     //pass tableview cell
     let vce = fullScreen()
    vce.text = UITableViewCell




           vce.modalPresentationStyle = .overCurrentContext // actually .fullScreen would be better
           vce.present(vce, animated: true)}

Solution

  • Assume you have an array titles

    cell.textLabel?.text = titles[indexPath.row]
    

    and then take action in this method:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    
        moveRight(titles[indexPath.row])
    
    }
    
    func moveRight(_ title:String) {
        //pass tableview cell
        let vc = fullScreen()
        vc.text = title
        self.navigationController!.pushViewController(vc, animated: true)        
    }