Navigate from a ViewController to an another on didSelectRowAt programmatically - swift

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"
}

Here is a complete answer:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
}

In UIKit in order to programmatically navigate from TableCell you can do it without storyboard.
You have two ways to view next viewController
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)
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)

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

Related

unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

I am new to SWIFT and trying to show some data into table view controller but when I press the button it shows the above error. Please correct me
import UIKit
var selectedPlace : Place!
class ShowPlaceTableTableViewController: UITableViewController {
var places : [Place]!
override func viewDidLoad()
{
super.viewDidLoad()
places = readPlaces()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return places.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlaceTableViewCell
cell.countryLabel.text = places[indexPath.row].country
cell.placeImageView.image = places[indexPath.row].picture
return cell
}
func readPlaces() -> [Place]
{
if UserDefaults.standard.object(forKey: "places") != nil
{
var data = UserDefaults.standard.object(forKey: "places") as! Data
let places = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as! [Place]
return places
}else
{
return [Place]()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedPlace = places[indexPath.row]
performSegue(withIdentifier: "detailSegue", sender: self)
}
}
Is your PlaceTableViewCell inside it's own xib separately OR is it inside the storyboard itself? You must register either a cell class or a nib before you try to dequeue the cell from tableView.
If it's inside the storyboard, then you can register the subclass to work with it.
tableView.register(PlaceTableViewCell.self, forCellReuseIdentifier: "PlaceTableViewCell")
If it's inside it's own xib separately, then you can register the UINib to work with it.
tableView.register(UINib(nibName: "PlaceTableViewCell", bundle: nil), forCellReuseIdentifier: "PlaceTableViewCell")
These need to be called once only, you can place these calls inside viewDidLoad().
Now you can dequeue it like following -
let cell = tableView.dequeueReusableCell(withIdentifier: "PlaceTableViewCell") as! PlaceTableViewCell
Note : The reuseIdentifier must be same in following places -
storyboard (or xib)
at the time of registration tableView.register...
at the time of dequeue tableView.dequeue...

How to fix segue showing a grey screen

I wanted to create a segue going from FirstViewController to DetailViewController, but when I write
let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailVC, animated: true, completion: nil)
, all it presents is a grey/transparent screen. How can I fix this?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostTableViewCell
cell.typeLabel.text = "Name: \(posts[indexPath.row].typeOfFood)"
cell.titleLabel.text = "Title: \(posts[indexPath.row].title)"
cell.locationLabel.text = "Location: \(posts[indexPath.row].location)"
cell.nameLabel.text = "Name: \(posts[indexPath.row].name)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// print("Select Row \(indexPath.row)")
var result = indexPath.row
tableView.deselectRow(at: indexPath, animated: true)
// self.performSegue(withIdentifier: "showDetail", sender: nil)
let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailVC, animated: true, completion: nil)
}
} ```
The problem is the phrase DetailViewController(). That's not you get the detail view controller you designed in the storyboard.
Instead, give the view controller you designed in the storyboard an identifier, and ask the storyboard for that view controller by calling this method:
https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller
Now cast that view controller down to DetailViewController and go from there.

storyboard elements not visible in view controller after segue

I added a view controller (vc2) with some simple form elements to it to my storyboard and made a segue ctrl-dragging from an existing tableviewcontroller (vc1) which is triggered by a trailing swipe button
I can see output from the print function (in the vc2 code below )in the debugger but my form elements aren't visible. And for some reason I had to manually set the background color, which had defaulted to black, not what was set on storyboard. I think this is related to the way I am loading vc2, but my attempts to change the code to a normal performSegueWithIdentifier caused a
'NSInvalidArgumentException', reason: 'Receiver () has no segue with
identifier
Then I deleted and remade the segue with no effect. So I changed the code back to this, which works but doesn't render the storyboard elements.
func clickView(forRowAtIndexPath indexPath: IndexPath) {
let myContents = UnitComponents[indexPath.row]
print("Clicked Report \(self.ProjectID) \(self.ShipListID) \(self.UnitName) \(myContents.sku)")
self.Sku = myContents.sku
let vc = ComponentViewController()
vc.Sku = myContents.sku
navigationController?.pushViewController(vc, animated: true)
}
Here is the vc2 code
import UIKit
class ComponentViewController: UIViewController {
var Sku = ""
var UnitName = ""
var ShipListID = ""
var ProjectID = ""
#IBOutlet var damageDesc: UITextView!
#IBOutlet var repairCost: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
print("Hello World \(Sku) \(UnitName) \(ShipListID) \(ProjectID)");
// Do any additional setup after loading the view.
}
#IBAction func saveReport(_ sender: Any) {
print("damageDesc \(String(describing: damageDesc ?? nil))")
print("repairCost \(String(describing: repairCost ?? nil))")
}
}
How can I fix my code so the storyboard layout etc gets loaded and I can see the form elements in the app?
Is this the wrong way to go to another view controller? I ask because it seems like some SO questions around this topic suggest it isn't calling the storyboard correctly. I'm searching for a swift 5 example of how to do this and only find references to instantiateViewControllerWithIdentifier("ViewController") which doesn't seem to be in swift 5
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = ShippingUnitsTableViewController()
vc.ShipListID = ShipLists[indexPath.row].ListID
vc.ProjectID = ProjectID
navigationController?.pushViewController(vc, animated: true)
}
you need to specify the storyboard in which the viewcontroller is located, and then instantiate the viewcontroller from that storyboard. Replace your func with the below one:-
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ShippingUnitsTableViewController") as! ShippingUnitsTableViewController
vc.ShipListID = ShipLists[indexPath.row].ListID
vc.ProjectID = ProjectID
navigationController?.pushViewController(vc, animated: true)
}
and make sure (inside the Main.storyboard file) you give ViewController's (which needs to be presented) Storyboard ID to be ShippingUnitsTableViewController in the attributes inspector (see the image below):-
can you try
func clickView(forRowAtIndexPath indexPath: IndexPath)
instead, didSelectRow of tableview delegate methods or view controller's performSegueWithIdentifier ?
Ok the solution to this problem required changing how I moved between view controllers. The didSelectRowAt indexPath method below does not utilize the storyboard, which causes the destination view controller to be sort of an orphan and unaware of the storyboard segues.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = ShippingUnitsTableViewController()
vc.ShipListID = ShipLists[indexPath.row].ListID
vc.ProjectID = ProjectID
navigationController?.pushViewController(vc, animated: true)
}
I changed didSelectRowAt indexPath to this, and added the bits of necessary info to the prepareSegue method as shown below.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.ListID = ShipLists[indexPath.row].ListID
performSegue(withIdentifier: "UnitsVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "UnitsVC"{
let nextViewController = segue.destination as? ShippingUnitsTableViewController
nextViewController!.ProjectID = self.ProjectID
nextViewController!.ShipListID = self.ListID
}
}

Swift 3 : Tableview select row goes to different navigation views

Trying to create a simple app with Swift 3, using Xcode 8.2. Would like to have a hierarchy as follows:
Tab-bar View
-->TableView
----->TableView
--------->View
--------->TableView
--------->etc.
----->TableView
----->View
-->TableView
For example, main tab bar should have a tableView controller embedded inside a navigation controller. Clicking on a certain cell (index.path) should lead to a separate tableview or viewcontroller that itself can branch off into its own series of 'child' tables. The tables should be dynamic, and all have navigation control that leads back 'pop' to the most previous table/view controller.
Currently, for the main tableview, I have:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == List_Database_Indexes.employees {
let myWebView = self.storyboard!.instantiateViewController(withIdentifier: "Employees") as! UITableViewController
//self.present(myWebView, animated: true, completion: nil)
self.performSegue(withIdentifier: "Employees", sender: nil)
}
This works to bring up the Employees UITableViewController but does not have the navigationbar controller at the top. For example, from apple, if you click on in the image below 'General' row, you are lead to a navigation tableview that takes you to Passlock code, which takes you to Auto-lock, all with navigation.
Change your code to:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == List_Database_Indexes.employees {
let myWebView = self.storyboard!.instantiateViewController(withIdentifier: "Employees") as! UITableViewController
//self.present(myWebView, animated: true, completion: nil)
//self.performSegue(withIdentifier: "Employees", sender: nil)
self.navigationController?.pushViewController(myWebView, animated: true)
}
}
This method is delegate method. so it will apply on all of the cell for table because we have bind delegate to tableview. cell is subview of table view.
you have to give condition for wether the cell is selected or not..
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == //index of that cell {
let Employee = self.storyboard!.instantiateViewController(withIdentifier: "Employees") as! UITableViewController
self.navigationController?.pushViewController(Employee, animated: true)
}
}

creating an action when tableView cell in swift touched or clicked

I a tableView class that displays a text in cells. I need to be able to touch/click text in a cell and create an action (display the next table view) based on the text in the cell clicked. Class is as follows:
import UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet var tableView: UITableView!
let textCellIdentifier = "TextCell"
let catRet = XnYCategories.mainCats("sport")
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
// MARK: UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return catRet.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
cell.textLabel?.text = catRet[row]
return cell
}
// MARK: UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println(catRet[row])
}
}
I would like to add upon Linus answer, as it has deprecated in latest Swift addition.
Swift 4:
In the myCell add this Recogniser,
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(yourVC.yourfuncName))
myCell.addGestureRecognizer(tapGesture)
In the same VC, implement your function,
func yourfuncName(){
//Do whatever you want here
}
If you have a hard-coded amount of cells, you can create a UIGestureRecognizer and add it to the cell created:
let tapGesture = UITapGestureRecognizer(target: self, action: "tapped:")
myCell.addGestureRecognizer(tapGesture)
In it's target you can do whatever you want to do.
You can get your cell using
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
To get your text you could use
var selectedText = currentCell.textLabel?.text