1

I have a UIViewController that contains a custom UITableView. The table has a custom UITableViewCell too.

How to navigate from the first ViewController to an another when you select/click one of the rows?

Note

I have not used StoryBoard.

Update

This my code. Each one of the classes are external file. Let me know, if you need more code.

class TestViewController: UIViewController, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(testTableView)
    }

    let testTableView: TestTableView = {
        let table = TestTableView()
        table.register(TestTableViewCell.self, forCellReuseIdentifier: TestTableViewCell.identifier)
        return table
    }()
}

class TestTableView: UITableView,  UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: TestTableViewCell.identifier, for: indexPath)
        return cell
    }
}



class TestTableViewCell: UITableViewCell {
    static let identifier  = "testCell"
}
mahan
  • 12,366
  • 5
  • 48
  • 83

3 Answers3

2

Here is a complete answer:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let newViewController = NewViewController()
    self.navigationController?.pushViewController(newViewController, animated: true)
}
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • 1
    You don't call this method, it is a delegate method. You need to set the delegate of your table view to be your first view controller. In `viewDidLoad` add: `self.tableView.delegate = self`. – Mo Abdul-Hameed Oct 27 '18 at 10:45
1

In UIKit in order to programmatically navigate from TableCell you can do it without storyboard. You have two ways to view next viewController

  1. If you want to present .......

animating from bottom to top (one thing to remember) you can not present new view without dismissing previous presented screen, otherwise app crash due to conflict of presented screen

yourTableView.deselectRow(at: indexPath, animated: true)//used for single Tap
let vC = YourViewController (nibName: "" , bundle : nil)
        vC.modalPresentationStyle = .fullScreen //this line is optional for fullscreen
        self.present(vC, animated: true , completion: nil)
  1. Or if you want to View your viewController normally (2 is batter) ....

animate from right to left

yourTableView.deselectRow(at: indexPath, animated: true)
let vC = YourViewController()
self.navigationController?.pushViewController(vC, animated: true)
Nah
  • 1,690
  • 2
  • 26
  • 46
gsk_fs
  • 94
  • 1
  • 10
0
 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let vc = yourCustomVCName (nibName: "yourCustomVC nibName" , bundle : nil)
     self.present(vc, animated: true , completion: nil)  
}
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
Mahgolsadat Fathi
  • 3,107
  • 4
  • 16
  • 34