在didSelectRowAtIndexPath之前调用viewDidLoad [英] viewDidLoad being called before didSelectRowAtIndexPath

查看:92
本文介绍了在didSelectRowAtIndexPath之前调用viewDidLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有
viewDidLoad (下一个tableView)在之前调用didSelectRowAtIndexPath (当前的tableView )。

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

我正在使用'singleton'来保存我的模型,到目前为止,这种方法运作良好。我试图将 didSelectRowAtIndexPath 中当前表中选择的行传递到目标 tableview (它们由以下链接:通过设置 didSelectRowAtIndexPath 中的变量来设置故事板中的segue。

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.

然而,目的地 tableview viewDidLoad 被调用在我可以设置行号之前。稍后调用 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.

推荐答案

使用segues时,您不需要 didSelectRowAtIndexPath:。您只需将segue链接到IB中的单元格即可。在代码中只需使用:

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;

这篇关于在didSelectRowAtIndexPath之前调用viewDidLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆