Swift segue negating other functions connected to segue object - swift

In a table view controller, I'm trying to get the index of the row that is selected before I segue (will pass this info in prepare for segue eventually). I have implemented this function:
var selectedQuestionCell: IndexPath?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedQuestionCell = indexPath
}
This was working great for me initially. I was able to snag the index of the selected row in my table. However, I want to perform a segue when a row is tapped which will transfer to another View. I connected a segue to my cell (dynamic prototypes). After connecting a segue, my override function no longer executes! I'm really new to Table View Controllers, so I'm not sure if there's anyway I can manually execute this.
To summarize: my problem is after connecting a segue to my cells, the override function to get their index is no longer called. How can I fix this?

Try these thing
Create a new segue from your tableview to another view and name it as you like.
Now add this code inside your didSelectRowAt
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController = storyboard?.instantiateViewController(withIdentifier: "your_segue_name_here") as! YourViewControllerName
self.navigationController?.pushViewController(viewController, animated: true)
}
Please make sure that your tableview is connected with navigation controller.
To do that select your tableView then click on Editor -> Embed In -> Navigation Controller from your Xcode.
I hope it works for you.
: D

Related

How to perform a segue from TableView programmatically

I have a TableView on a ViewController and want to segue to another ViewController by pressing any row. My Code for this is:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "ShowDetailView", sender: Cell)
}
I have connected both ViewControllers in the storyboard and named the storyboard segue identifier "ShowDetailView", but i get an error with this code?
Can somebody give me an advise?
It seems that you need to send clicked cell for getting the content , so replace
performSegue(withIdentifier: "ShowDetailView", sender: Cell)
with
performSegue(withIdentifier: "ShowDetailView", sender:arr[indexPath.row])
Where arr is the table's dataSource array , if you don't need to send anything do
performSegue(withIdentifier: "ShowDetailView", sender:nil)
Its because in the destination, “detailsViewController” there was a subView called ‘itemLabel’ that you either delete or rename it in code and forgot to disconnect its outlet from UI. Right click on it and hit X on itemLabel outlet

Navigate to a new view controller for every cell in table view

I am a beginner coder in swift. I am trying to create a tabbed application. For one of my tabs, I am creating a table view which has multiple rows each which have a different task (A good way to think of this is the facebook app, where each option in the more screen will take you to a separate view)
Now, my table is populated with an array:
let array = ["one", "two", "three]
I want to ask, that everytime that I tap on one of these rows, I would like to go a new view controller. How is this possible?
What I tried was performSegue with an identifier which I give in the storyboard, but then there would be an x amount of segues connecting to the table view? So I don't think this is right? :/
I know the contents of the array prior to generating the table, so If I know the array value, and the row being tapped, how can I navigate to a new view controller?
Edit:
When performing the segue between the controllers, I am using:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showView", sender: self)
}
This of course will only connect to the segue showView however, how can i add multiple view controllers?
You need to simply compare which row is selected in tableView and then perform segue according to it.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let segueIdentifier: String
switch indexPath.row {
case 0: //For "one"
segueIdentifier = "showView1"
case 1: //For "two"
segueIdentifier = "showView2"
default: //For "three"
segueIdentifier = "showView3"
}
self.performSegue(withIdentifier: segueIdentifier, sender: self)
}
Add the following function to your controller.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
navigationController?.pushViewController(NewController(), animated: true)
}
Using Static Cells in a UITableViewController
Summary
An alternative is to use a UITableViewController with static cells. If you know already know the menu then you can just create static cells for each item.
In the storyboard you can connect a segue from each cell to their respective view controllers.
Example

Swift DidSelectRowAt not firing

I have a project with a few different VCs...two of them have TableViews
The first one works perfectly fine, I have a custom view in there etc.
The second one is also custom and it fill the information in just fine, but unlike the first one...I cannot tap on it to segue to the next VC
I have the Delegate and DataSource set up properly. I have another VC on my project with a tableview and it works just fine. The first thing I can think might be preventing it from working is a UILabel takes up 90% of the cell, but I don't think that is it because I made the label small and tried tapping then and it still wouldn't hit the breakpoint.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = indexPath.row
selectedService = serviceArray[row].serviceID! as String
if selectedService != "" {
self.performSegue(withIdentifier: "fromCardDetailsToEditService", sender: self)
}
}
I do not know if it matters BUT...the VC that I am on is in a Tab Bar Controller and a Nav Bar Controller. Does it matter for the performSegue that I am in a Tab Bar? And that this button is not a tab bar item?
Did you have any GestureRecognizer on your UIViewController because in case of GestureRecognizer tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) does not called.
I figured out how to have my Gesture Recognizer AND the didSelectRowAt.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CardDetailsViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
This is what I already had, which was causing conflict.
To fix it, this simple line let me dismiss the keyboard AND be able to tap on my cells.
tap.cancelsTouchesInView = false

Swift leave a row selected after changing viewcontroller

i have a two view application. If i select a row a segue load the second view Controller. I want that when i return back to my first controller the row is is still selected. I have tried with this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.cellForRowAtIndexPath(indexPath)!
self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
but when returning back the row not remain selected. What's is the problem??
P.S.: I want no multiple selection, only one row at time.
If you are using a UITableViewController subclass as your source view controller, what I presume because you are overriding the table view delegate methods, you can use the flag clearsSelectionOnViewWillAppear on your view controller to get this behavior.
override func viewDidLoad() {
self.clearsSelectionOnViewWillAppear = false
}
Or you can set this in your Storyboard as well, when selecting the attributes selector of your source table view controller, you have to untick the Clear on Appearance checkbox.

How do I get different navigation controllers based on what disclosure indicator in swift

I'm very new to swift. I added disclosure indicators to my TableView. I want to make the different disclosure indicators lead to different view controllers. Can anyone help me with this?
Connect your ViewController with multiple ViewControllers in your storyboard giving each storyboard segue an identifier. Then call self.performSegueWithIdentifier when you have identified which cell the user has selected. For example:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
self.performSegueWithIdentifier("yourIdentifier", sender: self)
}
...
}