Sending Cell data to destinationViewController with prepareForSegue - ios

I have added a UILongPressGestureRecognizer to my UICollectionViewCell and created a segue, so by long pressing a cell the user segues to a ViewController.
I already have an ordinary segue using the didSelect method to display cell details, but the secondary long press gesture segue takes the user to a action screen to delete the cell.
Unfortunately when using the prepareForSegue to send the cell data to the destinationViewController I cannot use the standard let indexPaths = self.collectionView.indexPathForSelectedRow() method because I am creating my own selection when using a gesture recogniser.
So how can I send the cell data to the destinationViewController with prepareForSegue using the gestureRecognizer ?
if segue.identifier == "cellAction" {
let vc = segue.destinationViewController as! CellActionViewController
//This will cause an 'array index out of range error
let indexPaths = self.collectionView.indexPathsForSelectedItems()
let indexPath = indexPaths[0] as! NSIndexPath
//Here is how I would send the cell data
let selectedCell = Items[indexPath.row]
vc.selectedItem = selectedCell
}

So you need to access the particular cell that was pressed, and use this information to obtain its index path from within prepareForSegue(_:sender:).
The first thing to note is that sender parameter on the prepareForSegue method. I presume you are calling performSegueWithIdentifier(_:sender:) from your gesture handling function. Notice this also has a sender parameter, which is the same object that is passed to prepareForSegue. You can use this to pass your cell instance.
The second thing to note is that UIGestureRecognizer has a view property, which should most likely be the cell itself. Armed with this knowledge, you can pass the pressed cell instance to prepareForSegue:
func handleLongPress(longPress: UILongPressGestureRecognizer) {
if longPress.state == .Ended {
performSegueWithIdentifier("cellAction", sender: longPress.view)
}
}
Now, within prepareForSegue you can cast sender to your cell class, and get its index path using UICollectionViews indexPathForCell(_:) method:
override prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "cellAction" {
let vc = segue.destinationViewController as! CellActionViewController
if let cell = sender as? MyCell {
let indexPath = collectionView.indexPathForCell(cell)
let selectedItem = items[indexPath.row]
vc.selectedItem = selectedItem
} else {
fatalError("Invalid sender, was expecting instance of MyCell.)
}
}
}

Related

PrepareForSegue with UITableViewController - Pass data

I am having a huge problem with my code. My question is, how can I get the data from the tableviewcell the user clicks on to a detail viewcontroller.
I am already getting the data out of an array and displaying it in my tableviewcontroller, but how can I pass that data through to a new viewcontroller using prepareForSegue?
This is my code for displaying data in the cells.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "MealTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MealTableViewCell
// Fetches the appropriate meal for the data source layout.
let meal = meals[indexPath.row]
cell.nameLabel.text = meal.name
cell.photoImageView.image = meal.photo
cell.descriptionLabel.text = meal.description
cell.servedWithLabel.text = meal.servedWith
return cell
}
The problem is, that calling a prepareForSegue outside of that means that I cannot use for instance meal.name to pass the name through to the detail viewcontroller? What to do?
Hope you guys can help me - I have been struggling with this for some time now :-(
As pointed out by Arun, you need to use -prepareForSegue. Here's what I'd do to pass something to your detail view controller (say meal.name)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Use this to determine the selected index path. You can also store the selected index path in a variable using -didSelectRowAtIndexPath
let cell = sender as! UITableViewCell
let indexPath = self.tableView.indexPathForCell(cell)
// Get the relevant detail
let meal = meals[indexPath.row]
// Create reference and pass it
let detailViewController = segue.destinationViewController as! MyDetailViewController
detailViewController.mealName = meal.name
}
And eventually, just hook up the UITableViewCell with MyDetailViewController using your Storyboard (and select whatever presentation and transition style you prefer)
Edited for Swift 4
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let cell = sender as! UITableViewCell
let indexPath = self.tableView.indexPath(for: cell)
// Get the relevant detail
let meal = meals[indexPath!.row]
// Create reference and pass it
let detailViewController = segue.destination as! MyDetailViewController
detailViewController.mealName = meal.name
}
When you click a cell. It will call didSelectRow. In didSelectRow save the indexPath.row and do performseguewithidentifier
Now in prepareForSegue get the object from the array using meals[selectedIndex] and assign to detailViewController Objects.
Make sure segue is from ViewController to ViewController.

How to update selected row before segue?

I need to pass a variable containing the index of the selected row in one view to the next view.
firstview.swift
var selectedRow = 0;
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedRow = indexPath.row + 1
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "viewAssignmentsSegue") {
let navController = segue.destinationViewController as! UINavigationController
let controller = navController.viewControllers[0] as! AssignmentsViewController
print(selectedRow)
controller.activeCourse = selectedRow
}
}
My issue is that, when a row is selected, the selectedRow variable isn't updated by the tableView method before the segue occurs, meaning that the value is essentially always one behind what it should be. How can I delay the prepareForSegue until the variable is updated or how else can I successfully pass the selected row to the next view without delay?
One possibility: Don't implement didSelectRowAtIndexPath:. Just move that functionality into your prepareForSegue implementation. That, after all, is what is called first in response to your tapping the cell. Even in prepareForSegue you can ask the table view what row is selected.
Another possibility: Implement willSelectRowAtIndexPath: instead of didSelectRowAtIndexPath:. It happens earlier.
in your prepareForSegue:
controller.activeCourse = self.tableView.indexPathForSelectedRow
don't wait for didSelect to be called - react earlier.
Use this on prepareForSegue:
let path = self.tableView.indexPathForSelectedRow()!
controller.activeCourse = path.row
or
let path = self.tableView.indexPathForCell(sender)
controller.activeCourse = path.row

Passing variable (Int) to a View Control (Popover)

I have a tableView with 7 rows, each row is a different action. When a user taps on a row, a view controller is shown as a popover and a text field is displayed for them to add a note. I then want to save this note to Parse but I need to save the passedChildID with it and therefore it (passedChildID) needs to be passed from the tableviewcontroller. I have this working in other area's of my app from one tableView to another tableView but for some reason it won't work with the popover.
The following code is in SingleChildViewTableViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "childToNote" {
let popoverViewController = segue.destinationViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
let noteActionViewController = segue.destinationViewController as! actionNoteViewController
if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) {
noteActionViewController.passedChildID = passedChildID
}
}
if segue.identifier == "childToItemNeeded" {
//other stuff
}
}
This builds and runs fine, but when I tap on that particular row, I get the following error.
Could not cast value of type '<<app_name>>.SingleChildTableViewController' (0x10e1bef60) to 'UITableViewCell' (0x110bca128).
I have tried moving the let noteActionViewController... code above the let popoverViewController figuring that the former was never getting run but that didn't change anything.
The segue is being performed as followed (in case it makes any difference).
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
performSegueWithIdentifier("childToNote", sender: self)
}
}
I'm stumped because this code has worked elsewhere in my app without fail.
it looks like your problem lies in this line
indexPathForCell(sender as! UITableViewCell)
the sender in this case you are telling it to be self, which is a UIViewController
performSegueWithIdentifier("childToNote", sender: self)
You are then telling in the indexPathForCell line to cast it as a UITableViewCell but its of type SingleChildTableViewController. So change the sender to be the UITableViewCell
Maybe you can save the indexPath as a local variable and when you select a cell you change the value of the variable.

Custom CollectionView Cell PrepareForSegue

I have a custom UICollection View Cell. One of that cell's property's is cellImageView. When the user selects a cell, I want to pass the image from that view (cellImageView.image) to the next one. Here's what I've tried:
if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell)
{
if segue.identifier == "show"
{
var segue = segue.destinationViewController as! ViewPhoto
segue.imageURL = URLToPass
}
}
but quickly realized that's the wrong route to take. What's the best way to go about doing this? Thanks in advance.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
if let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as? GalleryClass
{
let imageToPass = cell.cellImageView.image
self.performSegueWithIdentifier(mySegue, sender: imageToPass)
}
}
And in your prepareForSegue function:
if segue.identifier == "show"
{
var segue = segue.destinationViewController as! ViewPhoto
// segue.imageURL = URLToPass
segue.image = sender as! UIImage
}
This way you're keeping code which contains CollectionView stuff in the own delegate method, which is easier to maintain if you have to change it in the future. And you only pass data in your prepareForSegue function which you actually need in there.

How to access segue in 'didSelectRowAtIndexPath'?

I know how to pass data using segues from prepareForSegue function, but the thing i have a TableViewCell from which there are two possible segues to two different ViewControllers (let's just say A, B as of now).
It was suggested here that it is best to connect the segues to View controller rather than the tableCell itself, which infact worked perfectly. But i want to pass information to the second View controller when the cell is clicked, So how to access segue that i connected to the Source ViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showQuestionnaire"{
if let indexPath = self.tableView.indexPathForSelectedRow() {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! QuestionnaireController
let patientQuestionnaire = patientQuestionnaires[indexPath.row] as! PatientQuestionnaire
controller.selectedQuestionnaire = patientQuestionnaire
self.performSegueWithIdentifier("showQuestionnaire", sender: self)
}
}
}
Again: This question is not regarding passing information through prepareForSegue
You should be using the didSelectRowAtIndexPath method to determine whether or not a cell was selected, and send the indexPath as the sender of the segue:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showQuestionnaire", sender: indexPath);
}
Then in your prepareForSegue method, get the indexPath from the sender parameter and use that to pass the correct row/data:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "showQuestionnaire") {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! QuestionnaireController
let row = (sender as! NSIndexPath).row; //we know that sender is an NSIndexPath here.
let patientQuestionnaire = patientQuestionnaires[row] as! PatientQuestionnaire
controller.selectedQuestionnaire = patientQuestionnaire
}
}
To explain...
I used the index path as the sender so I can easily pass the index path. You could also check for the currently selected cell using other UITableView methods, but I've always done it this way with success
You can't put performSegueWithIdentifier within the prepare for segue method, because performSegueWithIdentifier leads to prepareForSegue; You are just looping around and around with no aim. (When you want to perform a segue, prepareForSegue is always executed)
prepareForSegue doesn't run by itself when a row is selected. This is where you need didSelectRowAtIndexPath. You need a performSegueWithIdentifier outside of the method as described previously, which should be in didSelectRowAtIndexPath

Resources