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"
}
5
  • 3
    Possible duplicate of Swift programmatically navigate to another view controller/scene Commented Oct 27, 2018 at 10:24
  • What have your tried so far? Commented Oct 27, 2018 at 10:26
  • @MoAbdul-Hameed How do you select row from the controller? Commented Oct 27, 2018 at 10:26
  • @mahan You need to make your current view controller (the one that contains the table view) conform to UITableViewDelegate and implement its didSelectRowAtIndexPath method. Commented Oct 27, 2018 at 10:27
  • @RakeshaShastri Added some code. Commented Oct 27, 2018 at 10:44

3 Answers 3

2

Here is a complete answer:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let newViewController = NewViewController()
    self.navigationController?.pushViewController(newViewController, animated: true)
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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)

Comments

0
 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     let vc = yourCustomVCName (nibName: "yourCustomVC nibName" , bundle : nil)
     self.present(vc, animated: true , completion: nil)  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.