How to initially select a row in UITableView - 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;

Related

UITableView reloads on show view controller

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.

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.

UInavigation popping back view and updating previous view's content

I have a collection view with cells that can be tapped, and push to a second view. The problem is, the changes I make in the second view are suppose to alter the first view(collection view) when the user pops back to it. By adding a new cell with an image.I can't seem to get the content to update.
I tried to use viewWillAppear on the first view and [colectionview reloadData] it doesn't seem to work. Anyone got a fix for this? help would be greatly appreciated.
Have you debugged either UIcollectionViewDataSource methods are being called? Make sure you have set the delegate and datasource off collection view. What is the data source of collection view? Please paste your code of second view where you are making changes.
I fixed the problem By calling [collectionview reloadData] in viewDidAppear instead of viewWillAppear, only problem is there is a noticeable amount of lag before the cell appears. Anyone know a better way?

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.

Static UITableView in Storyboard, with programmatic segue

I have a storyboard that has a UITableViewController. The table view has static cells, and I've configured the cells to transition to other view controllers when tapped (by control-dragging them to set up segues). This works great.
Now I've added a row to the table, and for this row I need to transition to a UIViewController that's implemented in a different storyboard. I need to handle this one in code.
Is there a way to handle the tableView:didSelectRowAtIndexPath: for this one row in code, while letting the storyboard handle the rest of the rows which already have transitions defined?
I tried calling super within the tableView:didSelectRowAtIndexPath: to invoke the "default" behaviour, but that doesn't work (undefined selector on UIViewController). I want to invoke the storyboard's behaviour for all the rows except the one I want to handle programmatically.
Sure, you can do that. Implement didSelectRowAtIndexPath, and put in an if statement that executes the code you need to go to the other storyboard controller only for the row that you just added (and do nothing for any other row).

Resources