0

So I am trying to pass some data from the current indexPath.row between two view controllers. Both of the view controllers has this:

    var imageFile = [PFFile]()
    var imageText = [String]()
    var username = [String]()
    var createdAt = [NSDate]()
    var objID = [String]()
    var thumbEmoji = [NSArray]()
    var loveEmoji = [NSArray]()
    var laughEmoji = [NSArray]()
    var handsEmoji = [NSArray]()
    var pooEmoji = [NSArray]()
    var comments = [NSArray]()
    var tagString = ""

How can I pass this in the override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) and get the current indexPath.row?

Sibling Thor
  • 57
  • 1
  • 6

4 Answers4

1

This will give you the indexPath and the destination controller so you can set it's data. sender is also the UITableViewCell instance. This is for default implementation, where each cell triggers the ShowDetail segue.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow, let controller = segue.destinationViewController as? [your_destination_controller_type] {
            //Do you thing here
        }
    }
}
konrad.bajtyngier
  • 1,786
  • 12
  • 13
0

Just pass sender as indexPath, you have to convert it back at prepareForSegue method

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      [self performSegueWithIdentifier:@"identifire" sender:indexPath];
}

And in prepareForSegue method goes like :

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"identifire"]) {
        NSIndexPath *ip = (NSIndexPath *)sender;
        YourTargetView *gcVC = (YourTargetView *)[segue destinationViewController];
        gcVC.passdata = [YourArray objectAtIndex:ip.row];
    }
}

HTH!!

Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
0

let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

Mandeep Kumar
  • 776
  • 4
  • 18
0

I suggest moving your data model outside of your view controllers. That way, you don't have to pass anything. When a controller wants to change data, it tells the data model to update it. When a controller wants to display data, it asks the data model to provide it.

One piece of data model information then becomes "which is the current item".

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57