UITableView reloads on show view controller - ios

Please help me to understand why table view reloads on view controller start. I setup outlet for table via storyboard and data source, delegate. I did't call tableView reloadData method.
So When view controller starts '..numberOfRowsInSection' and other table view delegates methods called.

as Apple docs say:
UITableView overrides the layoutSubviews method of UIView so that it
calls reloadData only when you create a new instance of UITableView or
when you assign a new data source. Reloading the table view clears
current state, including the current selection. However, if you
explicitly call reloadData, it clears this state and any subsequent
direct or indirect call to layoutSubviews does not trigger a reload.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/
If you really do not need its reloading, create your tableView as a lazy variable and add it to a view programmaticaly when you need it.

Related

Modifying the scroll view function scrollViewDidEndScrollingAnimation

I wanted to override the function scrollViewDidEndScrollingAnimation for a reference to a tableView within a class.
In other words, I have a view and a UITableView within it, I dragged a reference to the table into the view controller and want to change the scrollViewDidEndScrollingAnimation for the same, is it possible to do so?
I have looked at previous responses to similar questions and they ask suggest making the class a scrollViewDelegate and implementing the function, but I have done so and it doesn't hit the breakpoint I have within the function even if I scroll through the table in the Simulator.
This method only gets called if the setContentOffset:animated: or scrollRectToVisible:animated: methods are called by you on the scrollView, and only if the animated parameter is set to true.
Scrolling the view in the simulator will not trigger the breakpoint in your implementation, but using either of those two methods will.
UIScrollViewDelegate Protocol Reference

UITableView not loading fully inside UIScrollView

I'm creating a ViewController that will contain as a portion of it a scrollView. In that scrollView I would like to include the view of another ViewController. When I set up this ViewController inside of the ScrollView, all of that ViewController's data is pulled from the web and even it's "ViewDidLoad" method is called. However, nothing appears except for the tableViewLines and a spinner I've created to show the page is loading. Here is what it looks like (the ScrollView in question is under Commitments and Awards):
What should be loaded inside the scrollView is a tableView that looks like this:
It may be that the tableview's delegate/datasource are not set correctly. Could you check whether tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: are called or not?
It is not a good idea to show view of one view controller in another view controller view. Apple does not recommend it. what ever you want to do, do it in the same view controller.

When UITableView's data is loaded? What's time tableview call its delegate methods. After viewWillAppear?

I am wondering when UITableview's data is loaded.
I mean, when I create a UITableView, then normally I will set its datasource and delegate. After that, this tableview will be added as a subview.
However, If I don't call reload method, I don't know when the tableview know it needs to ask its delegates and show the data. In another word, since a view has no viewWillAppear methods, what's the exact time that a tableview ask its delegate and show the data?
I've tried some what:
In viewDidLoad, I create a tableview, added it as subview, but not setting its delegates. Obviously, the data is not loaded.
Then I set its delegate once I tap a button, its data is not loaded.
But when the tableview's controller's viewWillAppear is called again (when the controller disappears and appears again), the data is loaded.
There is no universal set time, because a UITableView's display data is in cells, and the cells are dynamically loaded. (also probably reused) You can call reload, but that just instructs the table to refetch the data on visible cells. The question remains: why do you need to know? If you need to intercept the data before it's loaded into a cell, do it in cellForRowAtIndexPath, where you're supposed to stuff the data into the cell.

Update Table View on command from View Controller

New to iOS programing and objective-c and am getting stuck on something which I am guessing is simple. Here's my question:
I have a simple, single view application with two text fields, a button and a table view. The idea is that I want to take the text from the two text fields and populate the next available cell in the tableview with this text. I've got everything sorted out except for how to update the contents of the table view on command. Ideally, I'd like the table view to be updated upon pressing the button.
I have a View Controller which is serving as the data source and delegate for the table view.
I am aware of the [tableView reloadData] method, but it requires the appropriate tableView variable within scope. This is fine in say a delegate method like tableView:didSelectRowAtIndexPath, and I've gotten it update upon selection of any cell, but what if I want to update the table view when an action unrelated to the table view is performed, say when the button is pressed? There is no tableView variable in scope of this method to call reloadData from (or is there)?
So I guess my question is: is there a way to access the table view variable from the rest of the view controller for the purpose of calling reloadData OR is there a method I'm not aware of that would facilitate this?
Thanks for any help!
Why do you you create an outlet for your table view in view controller?
Your view controller will be delegate and data source for the table view (you should set in interface builder or from code:
self.tableView.delegate = self;
self.tableView.dataSoure = self;
I am assuming that self.tableView - is the outlet that I mentioned. When ou have an outlet you can easily update table view after pressing the button: e.g you add new value to an array that is used for filling table view and call
[self.tableView reloadData];
If you do not want to reload all table you can use method insertRowsAtIndexPaths:withRowAnimation:
Actually you have to read next manuals as well:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/AboutTableViewsiPhone/AboutTableViewsiPhone.html
This references are really useful.

How to initially select a row in UITableView

I can not find a good spot to call selectRowAtIndexPath:animated:scrollPosition: to initially select a row in UITableView. Table data have not been loaded when the table view controller is initialized, so I can't do selection immediate after the initialization of my UITableViewController(Over bound exception would occur otherwise).
You need to use viewWillAppear: to set the state of a view before it appears, every time it appears, even if the view has already been displayed previously. Any non-visible view in a UITabViewController or UINavigationViewController can be unloaded at any time, so you cannot count on a non-visble view to retain its state.
Or, for finer control, implement loadView, viewDidLoad, and viewDidUnload.
If you are maintaining your own hierarchy of view controllers you will have to manually forward the viewWillAppear, viewWillDisappear, etc., messages to your sub-view controllers.
Try and overload viewWillAppear:animated or viewDidAppear:animated make sure to call super as well.
Make sure to call selectRowAtIndexPath:indexPath animated:NO as well, it will select without the time delay.
you can set any cell selected in cellforrowatindexpath method of uitableview.By using cell.selected=YES;

Resources