Swift didSelectRowAtIndexPath and prepareForSegue - ios

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

Related

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

Passing data from table view controller to view controller

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

Why indexPath.row doesn't work here?

I am adding values from the first TableView var savedUrls to the first section of var detailsInSection in second TableView. In prepareForSegue, Xcode shows " unresolved Identifier for 'indexPath'. How should I revise the codes?
In first View Controller:
var folderNames = [String]()
var savedUrls = [String]()
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let url = savedUrls[indexPath.row]
self.performSegueWithIdentifier(segueToDetailsTable, sender: url)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == segueToDetailsTable {
let detailsVC = segue.destinationViewController as! DetailsViewController
detailsVC.detailsInSection[0].append(savedUrls[indexPath.row])
}
}
In Second View Controller:
var sectionTitles = ["WebSite", "Date Saved", "Document Used","Add Notes"]
var detailsInSection = [[String]() , [NSDate]() , [AnyObject]() , String]() ]
indexPath is not a variable defined or passed into prepareForSegue. You are on the right track though. Since you pass the url as the sender in performSegue..., you don't need an indexPath to reference the url. You can check that the sender was a String and use sender as the url that was selected.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let url = savedUrls[indexPath.row]
self.performSegueWithIdentifier(segueToDetailsTable, sender: url)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let url = sender as? String where segue.identifier == segueToDetailsTable {
let detailsVC = segue.destinationViewController as! DetailsViewController
detailsVC.detailsInSection[0].append(url)
}
}
Pass the index path in didSelectRow... as sender parameter and get the url later
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier(segueToDetailsTable, sender: indexPath)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == segueToDetailsTable {
let indexPath = sender as! NSIndexPath
let url = savedUrls[indexPath.row]
let detailsVC = segue.destinationViewController as! DetailsViewController
detailsVC.detailsInSection[0].append(url)
}
}

Preparing segue in Collection View

I have collection view implemented but have an error for the segue. I am trying to hand the image to another view controller using a segue. This is the code for it.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
self.performSegueWithIdentifier("showImage", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showImage"
{
let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
let indexPath = indexPaths[0] as NSIndexPath
let vc = segue.destinationViewController as! NewViewController
vc.image = self.imageArray[indexPath.row]!
vc.title = self.appleProducts[indexPath.row]
}
}
}
However I am having an error at collectionView!.indexPathsForSelectedItemssaying that Could not find member indexPathsForSelectedItems. Why is this happening and how should it be changed. Thank you
I have mode some changes to your code
Add following changes in your code
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
self.performSegueWithIdentifier("showImage", sender: indexPath)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showImage"
{
let indexPath : NSIndexPath = sender as! NSIndexPath
let vc = segue.destinationViewController as! NewViewController
vc.image = self.imageArray[indexPath.row]!
vc.title = self.appleProducts[indexPath.row]
}
}

Resources