简体   繁体   中英

iphone - numberOfRowsInSection being called before viewDidAppear

This might be normal, but I'm new to 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. After this is done, numberOfRowsInSection is called and it returns the count for the array and all is well. On the other view, after it is loaded first the numberOfRowsInSection is called and then viewDidAppear. I'm really new to the iphone and I have no idea why this is even happening.

Thank you!

This is normal. viewDidAppear is called after the entire view is has finished loading and became visible. This includes the UITableView . The Table View in turn requires the numberOfRowsInSection function to initialize.

You have three options to execute code before the UITableView loads:

1) Place your code in -viewWillAppear - this can occasionally be shaky

2) Subclass the UIViewController (chances are you are already doing this) and create a custom -init function to handle your setup

3) Put your initialization code inside of -numberofRowsInSection . This is always the first method of the UITableViewDataSource to be called. 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. A few hours of my life I'll never get back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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