Pass var from selected TableView Cell Swift Xcode - ios

I'm a beginner in Swift (Xcode) and I need help. I have a tableview and I want to send information from the indexPath.row to a segue.
My script work, but the problem is the data send to my segue is the previous selected row and not the current cell select. Because the function prepare segue is called before the func tableView. That's make 2 days I search and every tutorial give me the same problem.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
jobname = filldata[indexPath.row]
print(jobname)
performSegue(withIdentifier: "jobsegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "jobsegue" {
let destVC = segue.destination as! JobNameViewController
destVC.jobname = jobname
}
}
Thank you sooo much for your help!

Try this, this will work fine:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "jobsegue", sender: indexPath.row)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "jobsegue" {
let destVC = segue.destination as! JobNameViewController
destVC.jobname = filldata[sender]
}
}

Related

Stuck with "Type of expression is ambiguous without more context" in prepare function

I'm new to coding Swift, so please excuse me if this error is a simple answer..
I am trying to transfer data to another viewcontroller, but struggling with "Type of expression is ambiguous without more context" error.
I made a #IBoutlet var mainTableView inside of the Viewcontroller right now.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = mainTableView.indexPathForSelectedRow {
habitSelected = habitlist[indexPath.row]
}
Here's full code I made
https://i.stack.imgur.com/8mS4F.png
https://i.stack.imgur.com/Mzgwf.png
A alternative way to do that is to pass the row in the sender parameter
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "unwindTableViewControiler", sender: indexPath.row)
}
and it's recommended to check the identifier of the segue before unwrapping the sender
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "unwindTableViewControiler" {
let row = sender as! Int
habitSelected = habitlist[row]
}
}

I need to override a segue function after clicking a

So I have this tableView and I want when clicking a cell it gives me the name of that cell, and I pass it to next view, but the problem is that I need to override segue function inside of the "didselectrowAt" function and that is impossible I guess, any suggestion on how to do this?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
clickedYear = years[indexPath.row]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let ResumosVC: Resumos2ViewController = segue.destination as! Resumos2ViewController
ResumosVC.Title = clickedYear
}
Two solutions:
Reconnect the segue to the cell (rather than to the controller) and implement
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let cell = sender as? UITableViewCell,
let indexPath = tableView.indexPath(for: cell) else { return }
let resumosVC = segue.destination as! Resumos2ViewController
resumosVC.Title = years[indexPath.row]
}
Call performSegue in didSelect (replace the identifier string with the real value)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "MySegueIdentifier", sender: indexPath)
}
and change prepare(for to
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let indexPath = sender as? IndexPath else { return }
let resumosVC = segue.destination as! Resumos2ViewController
resumosVC.Title = years[indexPath.row]
}
clickedYear is not needed in both cases.
Note: Please conform to the naming convention to name variables with starting lowercase letter.

Prepare for segue inside of for loop

I have a table view with the following willSelectRowAt code:
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
for n in 0...carsArray.count - 1 {
if indexPath.row == n {
print(carsArray[n].name)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToEditCar" {
let name = carsArray[n].name
print(name)
let indexCar = n
let destinationVC = segue.destination as! EditCarViewController
destinationVC.name = name
destinationVC.indexCar = indexCar
}
}
performSegue(withIdentifier: "goToEditCar", sender: self)
}
}
}
Somehow the prepare function won't pass the desired data, neither will it print(name) - can anyone tell me the issue with this piece of code?
Your code cannot work at all. You are using the wrong API, prepare(for is never going to be called inside another method and actually you don't need a loop.
willSelectRowAt is to control if a cell is allowed to be selected. Return the indexPath if it's allowed otherwise return nil
This is not what you want. Use didSelect and pass the index path as sender when calling performSegue
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToEditCar", sender: indexPath)
}
In prepare(for get the index path from the sender parameter
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToEditCar" {
let indexPath = sender as! IndexPath
let name = carsArray[indexPath.row].name
print(name)
let destinationVC = segue.destination as! EditCarViewController
destinationVC.name = name
destinationVC.indexCar = indexPath.row
}
}
You don't need a for-loop here as n eventually will be equal to indexPath.row also you should use didSelectRowAt
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToEditCar", sender:indexPath.row)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToEditCar" {
let index = sender as! Int
let destinationVC = segue.destination as! EditCarViewController
destinationVC.name = carsArray[index].name
destinationVC.indexCar = index
}
}

How to pass data from NSObject to another ViewController when click on TableViewCell?

I have a ViewController1 with a TableView inside. What I want to do is go to ViewController2 and pass a piece of data along at the same time when user click on a TableViewCell.
For more details :
Each Cell have a itemId and other data which is store in NSObject class after pull from my API,which I use the data in TableViewCell class.So when I performSegue to ViewController2,I want to get the itemId in ViewController2
For now I able to go to ViewController2 when click on a tableViewCell by using this code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//here I dont know how to get the itemId for that particular cell
performSegue(withIdentifier: "GoToSecondVc", sender: self)
}
I know I can pass my itemId like this:
var itemId : Int!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GoToSecondVc" , let secondVC = segue.destination as? SecondViewController{
secondVC.itemId = self.itemId
}
The problem for now is I dont know how to get the itemId from NSObject when user click on any tableViewCell before I performSegue in didSelectRowAt section.
So how can I get the itemId in this case? Or is there a better way to do this?
I recommend to pass the selected index path in didSelectRowAt as sender:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "GoToSecondVc", sender: indexPath)
}
And use it in prepare(for segue, a temporary variable itemId is not needed. The code assumes that items is the data source array:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GoToSecondVc" , let secondVC = segue.destination as? SecondViewController {
let indexPath = sender as! IndexPath
let item = items[indexPath.row]
secondVC.itemId = item.itemId
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let it = dataArrray[indexPath.row] as! ItemType
performSegue(withIdentifier: "GoToSecondVc", sender: it.itemID)
}

Swift segue Issu -- Swift4 UItableView

After some time I got my segue working, but now when I press on a CollectionViewCell Its going just for a half second on a Blank TableView and then on the right one... I'm not sure what's wrong with my segue... Maybe someone knows what's the issue.
Video:https://drive.google.com/open?id=1RSMAjRKoFZw_hSY6KwpEADNN4z4BajMz
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDrawer", sender: fetchedResultsCtrl.object(at: indexPath))}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDrawer" {
let dst = segue.destination as! DrawerViewController
dst.cupboard = sender as? Cupboard
}
}
Thanks in advance

Resources