简体   繁体   English

iPhone-在viewDidAppear之前调用numberOfRowsInSection

[英]iphone - numberOfRowsInSection being called before viewDidAppear

This might be normal, but I'm new to objective-c. 这可能很正常,但是我对Objective-C还是陌生的。 I have an app with two view controllers, both are table views. 我有一个带有两个视图控制器的应用程序,两个都是表视图。 When a row is clicked, I load the second view to the top of the stack and pass a parameter through one of it's properties. 单击一行后,我将第二个视图加载到堆栈的顶部,并通过其属性之一传递参数。 On the first view (loaded by default by the navigation controller) the viewDidAppear function is called first, it opens an XML file from a URL, builds an object for each node and pushes all the objects in to an array. 在第一个视图(导航控制器默认加载)上,首先调用viewDidAppear函数,该函数从URL打开XML文件,为每个节点构建一个对象,并将所有对象推入数组。 After this is done, numberOfRowsInSection is called and it returns the count for the array and all is well. 完成此操作后,将调用numberOfRowsInSection,它返回数组的计数,一切正常。 On the other view, after it is loaded first the numberOfRowsInSection is called and then viewDidAppear. 在另一个视图上,首先加载numberOfRowsInSection,然后调用viewDidAppear。 I'm really new to the iphone and I have no idea why this is even happening. 我真的是iphone的新手,我也不知道为什么会这样。

Thank you! 谢谢!

This is normal. 这很正常。 viewDidAppear is called after the entire view is has finished loading and became visible. 整个视图完成加载并变为可见后,将调用viewDidAppear This includes the UITableView . 这包括UITableView The Table View in turn requires the numberOfRowsInSection function to initialize. 表格视图又需要numberOfRowsInSection函数进行初始化。

You have three options to execute code before the UITableView loads: UITableView加载之前,您有三个选项可以执行代码:

1) Place your code in -viewWillAppear - this can occasionally be shaky 1)将您的代码放在-viewWillAppear -有时可能会不稳定

2) Subclass the UIViewController (chances are you are already doing this) and create a custom -init function to handle your setup 2)子类化UIViewController(可能您已经在这样做)并创建一个自定义-init函数来处理您的设置

3) Put your initialization code inside of -numberofRowsInSection . 3)将您的初始化代码放在-numberofRowsInSection This is always the first method of the UITableViewDataSource to be called. 这始终是要调用的UITableViewDataSource的第一个方法。 It is hackish but works very well in practice. 它有点黑,但在实践中效果很好。

也许您应该考虑将代码移至-viewWillAppear-viewDidLoad

Thank you all for taking the time to reply! 谢谢大家抽出宝贵的时间回复! I found the issue. 我发现了问题。 Being new to xcode/iphone/mvc, I forgot to connect the view to the IBOutlet object. 作为xcode / iphone / mvc的新手,我忘记了将视图连接到IBOutlet对象。 A few hours of my life I'll never get back. 我一生的几个小时我再也回不到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM