Adding elements to top and bottom of UITable View Controller - ios

I am new to objective C. I have a small app where I have used a UITableviewcontroller(TVC). I need to add four elements.,label and button above TVC and a label and button below TVC. But it is allowing only to add 2 elements., one above TVC and other below TVC. Also added element is occupying the entire width of screen. Can I overcome this restriction???

Customize the table view cell – instead of using the default style of table view cell.
this link is helpful you with sample project
http://www.appcoda.com/customize-table-view-cells-for-uitableview/

These two are tableview properties.
tableHeaderView
tableFooterView
By using the above properties you can show your own custom view on top / bottom of table view.
sample code
[self.tableView setTableHeaderView:yourHeaderView];
[self.tableView setTableFooterView:yourFooterView];
Write this code in viewDidLoad.

UITableViewController is kind of a screwed up class, in my opinion. A UITableViewController can only manage a table view, and nothing else. If you want a more complex layout where you have a table view on part of the screen, and then other view objects elsewhere, you can't do that with a UITableViewController.
However, with iOS 6 and later, there is a pretty easy solution.
You can embed a UITableViewController as a child view controller of another view controller.
What you do is to create a parent view controller with everything you need. Then you create a container view on your parent view controller and size it to contain your table view. Then you create a separate scene in your storyboard that defines a table view controller. Finally, you control-drag from your container view to your UITableViewController, and create an embed segue.
I have a project on github that demonstrates this technique:
Demo project using UITableViewControllers as child view controllers

Related

Swift3, How to use same UITableView in different viewController

In my project, I have table including many cell prototypes, outlets and constraints already in xViewController class.
I would like to know how if I want to use this table again in yViewController class.
Is it possible or I have to create the new one?
You can use ContainerView for the same. You can take a separate UITableViewController or UIViewController as per your requirement.Let called this class CommonTableViewController.
In your XViewController , remove tableview and move all code to CommonTableViewController. Drag and drop container view from the object library on XViewController. You can delete pre- defined child of your ContainerView. Now add embed segue between XViewController and CommonTableViewController.
Drag and drop container view on YViewController as well and add segue as mentioned above.
Do let me know if you have any further queries.
Refactor your table view controller to its own dedicated storyboard (In Interface Builder: Select the table view controller, and choose on the Menu bar: Editor > Refactor to Storyboard...).
Make sure your table view controller is the "Initial View Controller" (has an arrow pointint to it from the left, on the storyboard).
Now, on the main view of every view controller on which you wish to display your table view, insert a Container View and replace the target of the Embed segue with a Storyboard Reference to the Table View stotyboard you just created.

Can not drag and drop UITableViewController into Storyboard

A book I am using is telling me to drag and drop UITableViewController in object library into Main.storyboard, but it's not working. The UITableViewController just rushes back to object library. What's the problem? It works fine with "Table View". I am using Xcode 7.2, and OS X El Capitan.
Please help.
If you can't drag a Table View Controller to IB, then don't do it. Use a Table View instead! They are (mostly) the same.
I guess your book is telling you how to create a simple table view. So let me tell you how to create a simple table view in a view controller.
Of course, you should first drag a view controller to IB. Then add a table view to the view controller. Add the constraints if you want. Then run the app!
You will probably see a few horizontal lines on the screen and you can scroll it. How nice!
Now how do you add contents to the table? Just create a view controller class file and associate it with the view controller you just created. Then make that class conforms to the UITableViewDataSource protocol and add the required methods. Finally, just associate the class with the table view's data source property. I think your book will talk about this in detail.
If you want to know more, visit https://www.youtube.com/watch?v=s78ndDj8K3U&list=PL6gx4Cwl9DGDgp7nGSUnnXihbTLFZJ79B&index=21
You can't drop a view controller on top of another view controller.
(This is why you are able to drop a Table View onto a view controller, but not drop a Table View Controller.)
Instead, drag the Table View Controller to a blank area of the storyboard. It will be droppable, and appear on the storyboard.
Note that Interface Builder changes the appearance of the droppable object to include a + sign when it's over an area where it can be dropped (and added to the storyboard).

Can't change the size of a TableView in Interface Builder

I'm trying to change the size of a tableView I have in interface builder and can't seem to. When I first started the app I could drag it around and stretch the sides but all of a sudden I can't adjust it. I tried to delete my tableView and add a new one but the same thing happened. Thanks in advance. Here's what I see when I try to change the size:
if your using a UIViewController you can drag and drop a tableView and can place in a custom position you want. but if your using a UITableViewController you cant move the tableView to your custom position. if you want to do it in UITableViewController you can do like below
if you want your tableView content should show from a point, that you want you can do this way,
[self.tableView setContentInset:UIEdgeInsetsMake(100,0,0,0)];
else if you want set your tableView to a frame in UITableViewController you do this way,
- (void) viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.tableView.frame = CGRectMake(0,100,320,300);
}
hope this will help you.
What kind of view controller are you using to manage your table view? Since you show "prototype cells", I'm guessing it's a UITableViewController. Those are built to fill the entire screen with a single table view (which has always annoyed me.)
Starting with iOS 6, though, you can create a "container" view in another view controler, and then drag an embed segue from your container view onto the table view controller. That does all the housekeeping to make the table view controller a child view controller of the other one, and then you can make it whatever size you want.
If you don't want to use a UITableViewController as a child of another view controller, you can use a regular view controller and wire up the data source and delegate methods yourself. However, things like static table views and prototype cells don't work then.
Hopefully this helps someone still coming across this problem. What I did was make sure the UIViewController had a UIView as its direct child, then dragged the UITableView as a child of the UIView, this allowed me to resize the UITableView.

How the view and viewcontroller hooked?

I cannot find where the view and viewcontroller get hooked? Is it in the xib file?
I learned that each viewcontroller can control several views, but where are those two get hooked?
I recommend you to read the whole ViewController Programming Guide if you have doubts like that:
ViewController Programming Guide
In case you want to jump right to your issue, check this section:
Resource Managment in ViewControllers
You can find a nice graph explaining where the views are created and linked in the ViewController:
A ViewController is just that, a class to manage the UIViews (there will be many) that it contains. The main view is automatically wired up for you and you are responsible for wiring up all the other views you add. Keep in mind that UIButtons, UILabels, UIViews, etc are all objects that inherit from UIView.
Like Antonio indicated, start with the Apple docs:
The view controller has its own view. Each child view (subview) view has a parent view (superview). You can nest views inside of views. In your case, the top view in the hierarchy is the view controller's view.
At design time, you can add a child view to any view in Interface Builder by simply dragging a new view onto the parent view. You can also adjust the view hierarchy from the Document Outline in Interface Builder.
When creating a view hierarchy in Interface Builder, the view hierarchy is stored in the .xib file.
At run time, your views are instantiated from the information in the .xib file, and each child view's superview property points to its parent view. Each view also has a subviews property that lists each of its child views.
You can add a view to any other view at run time by instantiating a new view and passing it to the parent view's addSubview method. Obviously, once instantiated, you can alter the view hierarchy by setting the superview and subviews properties and calling related methods.

Use a Collectionviewcontroller on a view inside main view

In my IPad Application I have a main view and it contains three child views
top
middle
and bottom
Is it possible to use different view controller on different child views?
For Example i want to use collection view controller on the bottom view and top view.
if yes then is it possible to add and delete cells dynamicially? A small example would be appreciated.
If you are using iOS 6 you can create a container view controller easily in a storyboard by dropping ContainerView from the object library.
Otherwise you will need to implement a container view controller manually. See the documentation.
To Use a collection View Controller on a view first you have to create a UICollectionViewController with its CollectionView somewhere else. you can create this CollectionViewController in interface builder or programatically. then treat the collectionView of the UICollectionViewController as normal View and add it to your View with desired frame.
If you are not familiar with CollectionView then take a look at some Tutorial on UICollectionView on Google
UICollectionView tutorial

Resources