Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewDidLoad being called before didSelectRowAtIndexPath

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.

like image 707
Ian Avatar asked Feb 20 '26 11:02

Ian


1 Answers

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;
like image 176
Mundi Avatar answered Feb 23 '26 01:02

Mundi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!