Passing data from table view controller to view controller - ios

I'm having issues with moving from a table view controller to a view controller to display information about a particular cell that was selected.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SentDetailView" {
let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
let referrals = referralsSent[indexPath.row]
let destViewController = segue.destination as! SentDetailViewController
destViewController.referringTo = referrals.referTo
destViewController.patientsName = referrals.patientsName
//print("Contained value during segue " + destViewController.patientsName!)
destViewController.patientsPhoneNumber = referrals.patientsNumber
destViewController.patientsEmail = referrals.patientsEmail
destViewController.comments = referrals.comment
}
}
Storyboard setup:
I get the following results:
I select a cell and it just highlights and doesn't do anything.

You should override didSelectRowAt and call performSegue with sender as the selected index path. Try this.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "SentDetailView", sender: indexPath)
}
And this.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SentDetailView"{
let indexPath = sender as! IndexPath
let referrals = referralsSent[indexPath.row]
let destViewController = segue.destination as! SentDetailViewController
destViewController.referringTo = referrals.referTo
destViewController.patientsName = referrals.patientsName
//print("Contained value during segue " + destViewController.patientsName!)
destViewController.patientsPhoneNumber = referrals.patientsNumber
destViewController.patientsEmail = referrals.patientsEmail
destViewController.comments = referrals.comment
}
}

Related

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

IOS/Swift: Pass Object in tableview to detail view controller

I am trying to grab an object from a tableview and pass it to a detail view, something I know how to do in Objective-C but am learning for first time Swift. However, my code is not getting the row from the index path or object.
Here is my code.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("segue")
if segue.identifier == "showDetail"{
if let destinationVC = segue.destination as? detailVC {
)
if let indexPath = self.tableView.indexPathForSelectedRow {
print("row%#",indexPath)//Prints as nil
let thisItem = myItems[indexPath.row]//never gets here
destinationVC.item = thisItem
}
}
}
}
Can anyone see why I am not getting the row or the object? Thanks in advance for any suggestions.
Edit:
I got it to work with following:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
let anItem: myItem = myItems[indexPath.row];
let destVC = segue.destination as? detailVC
destVC?.item = anItem
}
}
Implement UITableViewDelegate method,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = myItems[indexPath.row]
self.performSegue(withIdentifier: "showDetail", sender: item)
}
then implement below,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showdetail" {
let detailController = segue.destination as! detailVC
detailController.item = sender as! Your_Item
}
}
Why don't you use these methods to save the value of the selected row or item? and then perform segue?
tableView(_:didDeselectRowAt:)
tableView(_:didSelectRowAt:)

Button action segue doesn't work when prepareForSegue call in the same VC and VC contains tableView and anthers two segue action button

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndexPath = indexPath as NSIndexPath
performSegue(withIdentifier: "toAddToChart", sender: nil)
}
open override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.selectedIndexPath
let product = products[indexPath.row]
if (segue.identifier == "toAddToChart") {
if let viewController = segue.destination as? AddToOrderVC {
viewController.productName = product.productName
viewController.productCode = product.productCode
viewController.productType = product.productType
viewController.productPrice = product.productPrice
viewController.productDetails = product.productDescription
viewController.pImageUrl = product.productimageUrl
}
}
}
/when i remove prepareForSegue function from viweController its will work but when i apply it and after build the code i get thread 1:signal SigBArt error when another action segue button pressed
When let product = products[indexPath.row]
called out side of the segue with confirm identifier,it will beget an error so the of this error is
open override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.selectedIndexPath
if (segue.identifier == "toAddToChart") {
if let viewController = segue.destination as? AddToOrderVC {
let product = products[indexPath.row]
viewController.productName = product.productName
viewController.productCode = product.productCode
viewController.productType = product.productType
viewController.productPrice = product.productPrice
viewController.productDetails = product.productDescription
viewController.pImageUrl = product.productimageUrl
}
}
}

Usage of performSeguewithIdentifier Function in Dynamic Values Tableview

I can list datas in tableview. When I choose a row and trying to pass another view controller with showing specific data related to my pressed cell, It couldn't make it.
I didn't able to store data "selectedMeal" variable in prepareforsegue function, it always return [ ].
I think, my main problem is selecting the cell and sending to this cell to prepare for segue function. Perhaps, it has a problem in DispatchQueue.main.async function.
*When I try to pass to static data, it works great.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark;
DispatchQueue.main.async {
self.performSegue(withIdentifier: "gotoOrderDetail", sender: self)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "gotoOrderDetail") {
let DestViewController : OrderDetailListViewController = segue.destination as! OrderDetailListViewController
let selectedMeal = selectedCells.map { (index: Int) -> SavedMeal in
return savedMeal[index]
}
DestViewController.mealarray = selectedMeal
}
}
Try to use this code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "gotoOrderDetail") {
let DestViewController : OrderDetailListViewController = segue.destination as! OrderDetailListViewController
let path = self.tableView.indexPathForSelectedRow()! // get the selected indexPath
DestViewController.mealarray = savedMeal[path.row]
}
}
Try to use Sender parameter
you can you like this:
self.performSegue(withIdentifier: "gotoOrderDetail", sender: indexPath)
and then in prepare(for segue: sender?)
if (segue.identifier == "gotoOrderDetail") {
let DestViewController : OrderDetailListViewController = segue.destination as! OrderDetailListViewController
let index = sender as! Int
DestViewController.mealarray = savedMeal[index]
}
p/s: you do not need to use dispatch queue in this case

Swift didSelectRowAtIndexPath and prepareForSegue

I need to add a value to the selected row than in prepareForSegue that it does its action.But my prepareForSegue is doing the action before it.When i try to place the "performSegueWithIdentifier" it crashes,also i have added the id to the segue idenfifier in the storyboard.Im working with sqlite.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedRow = indexPath.row
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "editSegue")
{
let viewController : WorkoutView = segue.destinationViewController as! WorkoutView
viewController.workoutInfoData = marrStudentData.objectAtIndex(selectedRow) as! WorkoutInfo
navigationItem.title = ""
}
}
You can fetch the selected indexPaths from tableView in your prepare for segue
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "editSegue")
{
if let indexPath = tableView.indexPathForSelectedRow{
let viewController : WorkoutView = segue.destinationViewController as! WorkoutView
viewController.workoutInfoData = marrStudentData.objectAtIndex(indexPath.row) as! WorkoutInfo
navigationItem.title = ""
}
}
}

Resources