UITableViewController vs TableView - objective-c

What is the difference between dragging a Table View Controller into the storyboard vs dragging a UI View Controller and dragging a Table View inside that in xCode?
I know how to populate a table view controller in code.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath;
How do I populate a table view within a UI View Controller? What custom class do I delegate to the UI View? I cannot seem to put a tableViewController on it as it is only a ViewController with a table view...
I'd like to know this because I'd like to have objects other than the table in that view (i.e. a section of the view is for the table and the other section contains a label, an image, and a button.)

Populating a UITableView inside of a UIViewController is no different than populating a UITableView inside of a UITableViewController. You just have to make sure you implement the required datasource and delegate methods. You also need to be sure to assign the UIViewController as the delegate and the datasource for that UITableView.
If you want objects other than the table, then you should us a UIViewController. In fact, I rarely use the UITableViewController any more just in case I need to add other objects.

I found another difference too .
While using a UITableViewController , which has UIScrollView in it , the view scrolls up when keyboard moves up .
This doesn't happen with UIViewController as you need separate methods for View scrolling up and down .
You can also take a look at this
https://stackoverflow.com/a/14465669/5553647

Only UITableViewController can have static content. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW20

Related

Create NSTableView using Storyboard

My steps using storyboard
Create a Mac Cocoa app using storyboard. Drag a Table View from the right sidebar onto the storyboard.
Let the pre-existing ViewController to follow NSTableViewDataSource protocol. Implement two methods in ViewController.m: numberOfRowsInTableView: returns 10 and tableView:objectValueForTableColumn:row: returns #"321".
In storyboard, connect the Table View's delegate and dataSource to (the only) View Controller.
The Problem
Every cell displays "Table View Cell" rather than the data I want to fill. In spite of that, tableView:objectValueForTableColumn:row: was indeed called 10 times.
The alternative using .xib
https://www.youtube.com/watch?v=p5U94-uRCOo
Use .xib rather than .storyboard. Drag a Table View from the right sidebar onto the storyboard.
Create a class MyTableViewController who follows NSTableViewDataSource protocol. Implement the same two methods.
In xib, drag a new object to the object. Change its class to MyTableViewController and connect the object to the table view's dataSource.
Your table view needs to be cell based not view based. Loot at the documentation.
tableView:objectValueForTableColumn:row: is for cell based tables and
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row is for view based table views.

Change a UIViewController to a UITableViewController inside a storyboard?

I've made a view in my storyboard which I've now decided I'd rather display its data via static table cells.
I can't use static table views in a UIViewController (Static table views are only valid when embedded in UITableViewController instances). So, I need to convert my existing UIViewController to a UITableViewController somehow. I've changed the .h file's parent, but that hasn't done it.
Is there another way to get this going? I'd really rather not have to make a new VC in the storyboard and move everything over, it's a big hassle.
I'll add to this, since the question is about how to change a UIViewController into a UITableViewController, and given that this question is over a year old and the original answer, while valid and may or may not have been the only solution at the time, doesn't actually answer the question and is not the only solution.
It IS possible to do this, you just have to set up the table view delegate and datasource outlets in IB, and manually edit the storyboard XML, which sounds scary but is actually very easy.
First, change your class's parent to be a UITableViewController. UITableViewController already adopts the UITableViewDatasource and UITableViewDelegate protocols, so if your class does too you can remove them:
#implementation MyTableViewController : UITableViewController
...
#end
Next, create new referencing outlets on your UITableView for its dataSource and delegate. The easiest way to do this is to control-drag from the UITableView to itself. The popup will give you the dataSource and delegate options.
Lastly, you need to change the storyboard XML. The storyboard file can get pretty big pretty fast. The easiest way to find the scene you are looking for is by setting Storyboard Identifier in the Identity Inspector. To view the XML directly, right click on the storyboard file in the project navigator and select "Open As -> Source Code". Now just search for whatever you set the reuse identifier to earlier. You'll see something similar to this:
<!-- My Table View Controller -->
<scene sceneID="EuE-XX-cCb">
<objects>
<viewController storyboardIdentifier="MY_TABLE_VIEW_IDENTIFIER" ... >
// Lots of other stuff
</viewController>
</objects>
</scene>
All you need to do is change the opening and closing view controller tags
<viewController>
</viewController>
to be tableViewController instead
<tableViewController>
</tableViewController>
That's it! No need to create a new UITableViewController scene or embed a UITableViewController in a container view.
EDIT:
I should also add that the UITableView MUST be the root view. It cannot be embedded inside another UIView.
If you want your static cell table view not to take up the entire screen, then using a container view is the easiest way to go. Start with a regular UIViewController and drag a container view (next to normal UIView in the object list) into its view. Resize it however you want -- the storyboard will automatically provide a view controller connected to this container view with an embed segue. Delete that controller, drag out a table view controller and right-drag from the container view to this table view controller to make a new embed segue. This table view controller can be accessed from the UIViewController with its childViewControllers property (and conversely, you can access the UIViewController from the table view controller with parentViewController if you need to).
What I did, is creating a UITableViewController in IB, open the Storyboard with a text editor, and copy all the nodes inside from the UIViewController to the UITableViewController.
I think that with this way there's less risk of deleting something important.
Before copying the sections objects, make sure that both tableviews (UIViewController and UITableViewController) have the same properties set like: static or dynamic cells, style (plain or grouped), etc.

Objective C: How do I add custom table view cells to a subview of a UIViewController

I currently have a UIViewController displaying a map view. I would like to display some custom table view cells when I tap on a button within the same UIViewController. Can I do that? Or do I need to set the table view cells within a UITableViewController?
Thanks for any advise here.
Zhen Hoe
You should use a UIViewController subclass rather than a subclass of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, one of which is a table view. The default behavior of the UITableViewController class is to make the table view fill the screen between the navigation bar and the tab bar (if either are present).
I would like to display some custom table view cells when I tap on a button within the same UIViewController.
If you want to show your table view along with other views, you can add a UITableView subview and make your controller implement UITableViewDelegateand UITableViewDataSource protocols.
You need to create a UITableViewController, and then set the contents of the UITableViewCells using the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath delegate method.

UITableView within a UITableViewCell

I need to load a UITableViewCell xib into the root controller of a split view. The UITableViewCell contains a UITableView as a subview (designed in Interface Builder). This UITableView will be used to load other cells. Is the loading of the UITableView within the UITableViewCell possible to achieve??
In short - a cell with a tableview as a subview that loads data from DB.
I have tried this but with little success. The problem is that the delegate method...
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
..placed within the UITableViewCell implementation file, never gets triggered.
Please help.
Your design may be ill advised, however...
If you have code for your UITableViewCell class, then that object needs to be the data source and delegate for your embedded UITableView. Something, certainly, has to be the tableView's data source and delegate, of course.
Another best practice, in this case, would certainly to be to make sure that the tableView argument in the various data source and delegate methods is the tableView object you think it is before you do anything.
How many custom table cells with embedded table views are you planning? Why not use a single table view with Grouped style? Eacn section of the table can then contain the content that you are now putting into your UITableViewCell/UITableView element. Would certainly be simpler, and might achieve the same result.

Objective-C: How to send signal from view to view controller to change views?

I have a view controller that controls the switching between views. I would like one of the views to signal the view controller to switch to another view (and can't figure out how I can do this.)
To be more clear (hopefully): My view controller inserts a subview. That subview has a UITableView. I'd like, when you select a row in the UITableView, to remove the current subview and then switch to a different sub-view. Of course, I'd prefer the view controller to continue to keep track of which subview is loaded.
Does this make sense? (I'm still pretty green with Objective-C.)
Is there a way to send the view controller a message from the sub-view (that the view controller created)? Is there another way to accomplish this?
Thanks a bunch for the help... and I'd be happy to clarify if needed.
You might look into setting up a UINavigationController. Use the 2 UIViewControllers to control the individual views, and use the Navigation Controller to switch between the 2 views. From the UITableView, you can simply implement the method -
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Use this method to alloc the new view controller that you want to display
Then call the Navigation controller to push the new view controller onto the stack -
[self.navigationController pushViewController:controllerName animated:YES]
Finally, release the view controller that has disappeared.
This way the navigation controller keeps track of who is loaded, and can implement convenience functions like animating the transition. Also make sure to lookup the UITableViewController subclass - it is a subclass of UIViewController, but it provides some convenience functions for dealing with tables, like knowing when the user selects a particular row, and allows for the standard edit functions of most iOS apps.