0

After much searching here on SO, I have yet to find a solution which doesn't appear to be an argument of opinion. Previously in code, I could achieve my desired result with the below code (long route yes...)

if(indexPath.section == 0 && indexPath.row == 0) {
DetailViewController *dvc = [[[DetailViewController alloc] init] autorelease];
dvc.detail = data.city1;
[self.navigationController pushViewController:dvc animated:YES];

}

if(indexPath.section == 0 && indexPath.row == 1) {
DetailViewController *dvc = [[[DetailViewController alloc] init] autorelease];
dvc.detail = data.city2;
[self.navigationController pushViewController:dvc animated:YES];

and so on, providing each cell a specific action to execute upon selection and pass an array to the next view. All of my research is looking like a difference of style and opinion mixed with inconsistencies between hooking segue to the view controller or tot a specific cell and which method to use between didSelectRow or prepareForSegue. The following code is hooked to a cell in storyboard, so each cell in that section now passes the same data to the next tableview.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"shows"])
{
    ShowsList *viewController = segue.destinationViewController;
    viewController.detail = data.city1;
}

}

My question is how to handle indexPath.row in prepareForSegue to achieve what I could previously do in code and pass a specific array depending on which row is chosen? Ive tried reverting to a mix of storyboard and code which is a failure, creating multiple custom cells which seems to defeat the whole purpose of a "no mess" approach, and multiple variations of different answers. Again, how would I say "Hey, when this is chosen pass this info, when this is chosen pass this info...?" using the prepareForSegue method

  • possible duplicate of [Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?](http://stackoverflow.com/questions/8130600/use-didselectrowatindexpath-or-prepareforsegue-method-for-uitableview) – BJ Homer Jul 20 '14 at 21:46
  • @BJHomer Im not sure its a duplicate, Ive checked that before and doesn't appear to be what I'm looking for. Maybe I'm not clarifying myself properly or I'm misunderstanding. With that method i don't see where i declare what data to send when a specific cell is chosen, it throws errors when I "plug and play" which isn't what I'm asking for either. Using didSelectRow throws errors, using prepareForSegue I'm not able to declare what data to send depending on which cell is chosen. Choose city1, load this plist, choose city2 load this other plist. The answer isn't in your suggestion –  Jul 20 '14 at 22:11
  • As I understand it, you question is basically "how do I figure out which row was selected in prepareForSegue, like I would in didSelectRowForIndexPath". The linked question answers that question. Is there something else you're wondering about that I'm not understanding? – BJ Homer Jul 20 '14 at 22:16
  • @BJHomer I think its more that I'm not communicating what I'm looking for in proper terms. I believe I've stated my objective and problems with the linked question, as well as in the original question/ramble. If my terminology threw you off ill try simple terms again. City1 lives in index0, city2 lives in index1. In prepareForSegue how would i say "When city1 is chosen pass plistA to the next view, when city2 is chosen pass plistB to the next view" –  Jul 20 '14 at 23:46
  • Exactly the same way you did in -didSelectRowAtIndexPath; the only difference is that the segue allocates the destination controller for you. You've phrased this question as comparing -prepareForSegue with -didSelectRowAtIndexPath. The logic for choosing a plist based on the row number does not change when you start using segues. – BJ Homer Jul 20 '14 at 23:51

1 Answers1

0

The sender parameter in prepareForSegue:sender: will be the cell you selected, so you can get the indexPath like so,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender {
       NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
       // use the indexPath to query your array
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thank you, that was what I needed to get going in the proper direction instead of a plug and pray, guess and pick at existing code to figure out why its not working. –  Jul 21 '14 at 01:10