3

I have viewDidLoad (of next tableView) being called before didSelectRowAtIndexPath (of current tableView).

I am using a 'singleton' to hold my model and up to now this has been working quite well. I am trying to pass on the row selected in the current table in didSelectRowAtIndexPath to the destination tableview (they are linked by a segue in storyboard) by setting by the variable in didSelectRowAtIndexPath.

However the viewDidLoad of the destination tableview is called before I can set the row number. didSelectRowAtIndexPath is called later but by then I have set up my data (wrongly) for the destination table.

Yama
  • 2,649
  • 3
  • 31
  • 63
Ian
  • 71
  • 3
  • Can you post some of your code for this? – Ernő Simonyi Jan 26 '12 at 09:51
  • Yes, please post some code to explain this further. It seems to me like something is going wrong with your segue (if indeed you have one) more details needed please. – GWed Jan 26 '12 at 09:57

1 Answers1

5

When using segues you do not need didSelectRowAtIndexPath:. You just link the segue to the cell in IB. In the code just use this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   CustomViewController *newVC = [segue destinationViewController];
   newVC.property = theDataYouWantToPass;
}

You can retrieve your row through the sender variable that is passed.

UITableViewCell *cell = (UITableViewCell *) sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSInteger myRow = indexPath.row;
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks Mundi, perfect. I was beginning to think I had done something really naughty. Why don't I need to use didSelectRowAtIndexPath though? – Ian Jan 26 '12 at 10:27
  • That is the legacy method, if you will. You could delete your segue in IB and use `didSelectRowAtIndexPath` instead. When using segues it is simply not necessary - or you can do different things with it. – Mundi Jan 26 '12 at 10:31
  • i wish to send the selected row data, so how? – neobie Dec 10 '12 at 15:39
  • As described above, line 3. – Mundi Dec 10 '12 at 21:07