viewDidLoad being called before didSelectRowAtIndexPath

喜夏-厌秋 提交于 2019-12-19 09:52:28

问题


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.


回答1:


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;


来源:https://stackoverflow.com/questions/9016144/viewdidload-being-called-before-didselectrowatindexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!