How to pass an Index Path to another table

Say you have a central array somewhere and would like display this in more detail via two table view controllers. Instead of passing only the relevant information, you want to pass the entire index path from table1 to table2 and beyond.

You can pass data in the prepareForSegue:sender method and let the Storyboard to the heavy lifting:

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

    Table2 *table2 = segue.destinationViewController;
    table2.myIndexPath = [self.tableView indexPathForSelectedRow];
}

Here we instantiate a new Table2 object on which myIndexPath is a property. Then we set it to our own table’s selected indexPath. In Table2 we can then pick it up and read out the array’s property.

Note that the didSelectRowAtIndexPath method of the table view can be completely empty for the data to be passed on. In fact, should you try an pass data in this method it won’t be available until the table is drawn.





Leave a Reply