How to Get Indexpath in Prepareforsegue

How to get indexpath in prepareForSegue

Swift 3.0 / iOS 10

tableView.indexPathForSelectedRow was introduced in iOS 9

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = tableView.indexPathForSelectedRow{
let selectedRow = indexPath.row
let detailVC = segue.destination as! ParkDetailTableVC
detailVC.park = self.parksArray[selectedRow]
}
}

UICollectionView Getting indexPath in prepareForSegue

You can set sender as anything you want. This may help:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView!.cellForItemAtIndexPath(indexPath) as! ImageCollectionViewCell
performSegue(withIdentifier: "collectionSegue", sender: cell)
}

Get indexPath for segue

Either you didn’t pass anything as sender in the performSegue method or the thing you passed is not an instance of UITableViewCell or it’s subclass.

Accessing a Table View IndexPath from prepareForSegue

Place a breakpoint on the let indexPath = ... line. See if the MainTableView object is nil or not, since you are likely crashing because it is force unwrapping a nil object. If it is nil, check your connections to your storyboard / xib file.

How to pass indexPath value from performSegue() to prepareForSegue()

In my opinion it isn't very good practice to pass the index path (or any other value that counts a s "data") as the sender argument; as its name suggests, it is intended for passing the object that sent the message (i.e., called the action method), in this case self (you could "relay" the original sender if your action method calls another action method instead, but that's off-topic here).

As @sCha kindly pointed out in the comments, the Apple documentation on this method in particular, though, seems to leave room for doubt nevertheless. The parameter name sender clearly comes from the homonimous argument in all control actions that follow Cocoa's target/action pattern.

My suggestion:

The best you can do I think is to store the index path in a property of your view controller:

var selectedIndexPath: IndexPath? 

...set it on tableView(_:didSelectRowAt:):

if selectedNode.isLeaveNode() {
self.selectedIndexPath = indexPath
performSegue(withIdentifier: "userInput", sender: indexPath)
} else {
self.selectedIndexPath = nil
// ...

...and retrieve it (while resetting the property) in the prepareForSegue(_:sender:) implementation of the target view controller:

if let vc = segue.source as? SubmenuViewController {
if let indexPath = vc.selectedIndexPath {
vc.selectedIndexPath = nil // (reset it, just to be safe)

// Use indexPath...
}
}

Get index of selected row to use in prepare for segue

You must not change the signature of prepare(for otherwise it's not going to be called. The sender parameter must be Any?

Cast the sender parameter to IndexPath and I recommend a switch statement

func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == toDetailVCSegue {
let destination = segue.destination as! DetailViewController
let indexPath = sender as! IndexPath
switch indexPath.row {
case 0: destination.itemArray = namesArray
case 1: destination.itemArray = scoresArray
case 2: destination.itemArray = timesArray
case 3: destination.itemArray = completedArray
default: break
}
}
}

How to get indexPath of a custom cell in PrepareForSegue method - iOS

You have to set tag on the button. In your case, set the button tag as indexPath.section in CellforRowAtIndexPath.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFeedCell" forIndexPath:indexPath];
if (cell) {
cell.item = _array[indexPath.section];
}

cell.button1.tag = indexPath.section;
return cell;
}

In PrepareForSegue, get the index from button tag.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"AudioComment"])
{
NSInteger index = ((UIButton *) sender).tag;
NSLog(@" %ld ",index);
}
}

How to get the indexPath of tableView and use the indexPath on the metod prepare(for segue: )

I think you want to just perform the segue inside the tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) function...

override func tableView(_ tableView: UITableView, 
didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
performSegue(withIdentifier: "caja", sender: nil)
case 1:
performSegue(withIdentifier: "whatever you set this identifier to", sender: nil)
// cases for all the different rows...
}
}

If you only have one segue and you need to switch the variables of the view controller it shows differently based on the cell selected

overrider func prepare(for segue: UIStoryboardSegue, sender: Any?){
if let indexPath = tableView.indexPathForSelectedRow,
let destination = segue.destination as? CajadeAhorroViewController {
switch indexPath.row {
case 0:
// destination is the "CajadeAhorroViewController", set its properties for how you want it.
case 1:
// ... all 7 cases ...
}
}
}


Related Topics



Leave a reply



Submit