Swift3, How to use same UITableView in different viewController - ios

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.

Related

Can you define a Segue in the storyboard to self?

I have an app that represents a folder structure. I have a UITableViewController for the folders and files listing, and a UIViewController for the Documents.
I want to be able to recursively navigate through the folder structure, so I want to reuse the Folder UITableView multiple times while I let the user drill down a folder structure.
Is there a way to draw a segue from the UITableViewController to self so when I select a folder I present another instance of the view, but with the content of the subfolder?
I did this in previous versions of Xcode, but I cannot figure this out on Xcode 9.
You can use Storyboard Reference and Storyboard ID of ViewController in Interface Builder
Screenshot
Yes you can do it. Add a hidden button in view controller and drag & drop segue self view controller.
I've never created a segue link to the same view controller, but based on Halil's answer above, it sounds like it's possible.
Rather than messing with hidden buttons, though, why don't you give your scene a storyboard identifier, and then instantiate and push/present your view controller through code? You could put your logic in the table view's tableView(_:didSelectRowAt:) method.

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).

Embedding TableView in TableViewController into another view

I would like to customize content of table view controller(add some buttons, etc.) but root view for that is table view. Is it a way to embed it into another view and add controls on it?
You can use Container View.
In the storyboard create another view controller that you would use to host those controls. Then drag in Container View from the Object Library (normally the last item in the list) and place it where you want the table view to appear (which could be the whole screen). Doing this, the Container View will create another view controller. Simply delete that and ctrl drag from the Container View to your table view controller and select embed segue.
By doing this, you would not have to change anything in your current table view controller if it doesn't depend on other view controllers to give it data in order for it to work. If it does, however, you could assign the segue identifier to the embed segue, implement prepareForSegue:sender: and do whatever you would normally do with other segues.
You simply add TableView as a subview of a uiviewcontroller. Remember to call for delegates. Normally i don't use storyboards, but if you do it programmatically you can do like this:
On .h file:
#interface EmbeddedTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
And on .m file simply create your tableView by adding it as subview of viewcontroller.
You can look at this for example: How to set up UITableView within a UIViewController created on a .xib file

can't drag a tableview or scrollview into a storyboard viewcontroller

I have an existing view controller derived from UIViewController that I'm trying to drag a view into but the storyboard editor won't let me. If I create a new viewcontroller in storyboard it will let me and I notice it has some autolayout controls... something the original viewcontroller doesn't have.
I don't want to delete the original view controller and recreate since it already has a lot of the push segues and what not hooked up to it. How can I get it to allow me to drop views/tableviews onto the storyboard surface of the viewcontroller?
Have you try to :
Copy your controller into a new storyboard and test to add a view ?
Add other element ? Likee UIButton, UISwitch etc... Can you do it or not ?
Have you try to add it like that (drag & drop on view hierarchy) ?
Autolayout is a way to manage how views are placing comparing to others views.
You can activate or desactivate by clicking on your viewcontroller and by clicking this checkbox, it will affect to whole storyboard :

Adding elements to top and bottom of UITable View Controller

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

Resources