IB - add UIView to UITableViewController? - ios

I like to accomplish an UIToolbar below an UITableView and I wanted to use the UITableViewController because it sets up the data source and delegate and other stuff on its own.
I don't need an UINavigationController as has been proposed in similar topics due to only having 1 view currently and my data is without a hierarchy.
I didn't manage to drag and drop an UIView or UIToolbar to my UITableViewController at any place (scene design area or view hierarchy) in Interface Builder of XCode 4.2.
Thus my question: How to add an UIView to an UITableViewController in Interface Builder?
I did manage to achieve the look I intend to accomplish using an UIViewController with an UITableView and an UIToolbar contained in its UIView.
Would using an UIViewController with an UITableView be much more involved than relying on the UITableViewController?
Thank you for your time!

I think this is your real question
Would using an UIViewController with an UITableView be much more
involved than relying on the UITableViewController?
The answer is no, its not much more work. Just add this to the viewcontrollers' .h
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
Then in the .h add the datasource and delegate functions( you could just copy and paste the functions your'e currently using in your TableViewController)
NOTE: From Xcode 4.5.1: "Static table views are only valid when embedded in UITableViewController instances."

Related

Swift how to create an effect similar to it of UIPageViewController between a UIViewController and UITableViewController

I am working on a project involving scrolling between a UIViewController and a UITableViewController. I tried to use a UIPageViewController to hold a UIViewController and a UITableViewController, but the .setViewControllers function kept giving me an error.
Can someone give me a sample code for creating something similar in effect to a UIPageViewController between a UIViewController and UITableViewController?
I tried to use a UIPageViewController to hold a UIViewController and a UITableViewController, but the .setViewControllers function kept giving me an error
Nevertheless, a UIPageViewController is an excellent way to do this. You should concentrate your efforts on that.
Alternatively, as you have only two view controllers in play, you could make your own custom parent view controller with the UIViewController and UITableViewController as children, and display their views in a UIScrollView yourself.
As a final alternative, don't use a scrolling interface: use a UITabBarController instead, with the two view controllers as its children. This is easy to configure (you can do it directly in the storyboard). It is possible to make a UITabBarController horizontally scrollable, and I have explained elsewhere how to do it, but the topic is rather advanced.

What does one gain from using a UITableViewController?

Why not use a UIViewController with a TableView embedded in it?
UITableViewController has three properties:
tableView;
clearsSelectionOnViewWillAppear;
refreshControl.
Also, you can create Static Cells in Interface Builder only in UITableViewController.
If you need anything from above - use it instead of UIViewController subclass.
UITableViewController is a "shortcut" that is useful in situations when you need a simple table with static data, archived in a NIB/Storyboard. Using UITableViewController lets you get most of the behavior for free, without the need to write and manage a special data source for it.
Other than that situation, UITableViewController gives you very little on top of UIViewController with an embedded UITableView.

providing common functionality for different viewcontrollers in objective c

I have many viewcontrollers which needs to have some common functionality related to navigation.
Earlier I made a base class BaseViewController(extending UIViewController) which have all common functionality (like doing some tasks on viewDidLoad etc) and all my viewcontrollers extends BaseViewController.
The problem is that some of my viewcontroller should be subclass of UIViewController and some of UITableViewController, so I can not use above approach.
One way could be to write base class for both and duplicating code. Is there any better way without duplicating code.
While you can get around this by using delegation or helper objects, I would make the case for just not using UITableViewController. It is only a very light subclass on top of UIViewController, providing a table view, conforming to the delegate & data source protocols, and adding a property or two for selection & refresh.
While I wouldn't normally suggest recreating something that the framework has already done for you, it may (in your case) make your code more easy to understand if you just keep everything inheriting from a common base class and add a table view to one of the subclasses.
If you do think this would be a reasonable approach, the UITableViewController documentation overview gives a detailed description of exactly what & where these behaviours are implemented, so mimicking its exact setup is trivial.
Adding a table view to UIViewController
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) IBOutlet UITableView *tableView;
#end
In your storyboard, drag a "Table View" from the object library and drop it on top of your View Controller scene's "View" in the Document Outline - this will replace the root view with a UITableView.
Then just hook it up:
ctrl-drag from the view controller to the table view to hook up the view and tableView outlets
ctrl-drag from the table view to the view controller to set the delegate and dataSource outlets.
Done - no magic required.

Create Relationship Segues

How can I create a relationship segue? I would like to create a UIViewController subclass similar to UITabBarController or UINavigationController where, using Interface Builder, I can control + drag from a view controller to another view controller. I have tried
#property (nonatomic) IBOutlet NSArray *viewControllers;
#property (nonatomic) IBOutlet UIStoryboardSegue *root;
and also tried dragging a Container View into my view controller. When I do that, I can drag from one view controller to another but I cannot drag to more than one view controller. I also cannot find any documentation for a UIContainerView object.
Relationship Segues are handled by Interface Builder. You cannot create them manually if the starting view controller is not one of those you mentioned. The simplest solution for your issue is to create a TabBarController and hide its tab bar in code.
Here is a quite advanced tutorial on something very similar to what you are trying to do. You may get some more ideas from it. Advanced Storyboard Techniques
EDIT:
Thanks for the tip about using a TabBarController, but I am asking this question because I am trying to subclass UIPageViewController so that I can create the PageViewController's datasource from IB
That's an interesting idea, and here is an explained solution for that: Using UIPageViewController in storyboards You don't have to subclass the UIPageViewController, it is against the recommendation in the documentation, too. Create a class that implements the UIPageViewControllerDataSource delegate. Place a "green cube" in the page view controller's listing panel and set its class to be newly created one. Then drag from the datasource outlet to this cube.
However, the pages cannot be set up visually in this way or any other. It is unfortunately not supported at all.

Change TableView Size When It has AN XIB

My UITableViewController class uses an .xib. I would like to make the TableView only take up half the size of the screen, so that I could add more to it. Is there anyway to do this, or do I need to create it all in code, and not use .xib.
This thread covers a similar situation. In practice it would be easier to use a standard UIViewController and implement the UITableViewDataSource and Delegate methods manually.
you cannot resize the UITableViewController class,if you want to add something under table place it at it's footer.
You Can also declare UITableView in UIViewController through XIB through which you can resize it.
in .h File
IBOutlet UITableView *tView;
and connect it to file owner
Thanks

Resources