Same UITableView in different viewcontrollers? - iphone

I have an application that has multiple viewcontrollers and one common menu table.
Menu table is same in all viewcontrollers. Currently, If i want to add some more menu in my menu table then I have to change in all the view controllers.
Can we have a common table view which can be called in different viewcontrollers?
Thanks

Make a subclass of UITableView and create your menu in that like :
#interface menuTable : UITableView
Then you can use the instance of that class in all viewControllers.

Yes, you can.
When you add UIView to some other UIView, former will be removed from it's Superview and added to latter as subview. Remember to be careful with view's frame. e.g. convert it using convertRect:toView: selector

As I understand ur question, just create a tableView in CommonView (a ref. name) and as per your need you can call that table view. By this way you have to not change many times in your code.

Related

Implementing another table view in a UITableViewController ios

I need to add a new table view to my UITableViewController which will includes a different data and design.
the above prototype cells is the new table view I've added in storyboard, and the main table view is the second one which includes (title, label, and image).
Is it allowed to create another table view in UITableViewController, or should I create a UIViewController and implement the two tables in it???
In fact, I've finished the implementation of the first table view controller, so I need to implement the new one.
the attached image is showing what I want:
You will be better off with a UIViewController that has two tableViews as subviews if you want both of your tables to be shown at the same time. UITableViewController should be used only when there is just one tableView shown at a certain time.
You can either have one UIViewController being the dataSource and delegate for both of the tableViews, or have a UITableViewController for each of your tables and add them as subviews to a UIViewController that will be a container view for both of them. The latter will probably save you some time debugging possible issues as having two tableViews with a single delegate/dataSource requires a lot of duplicate code in the same place.
I think you should use UIExpandableTableView
there is detailed usage on github page
Scree example of UIExpandableTableView
Are you looking to have them be two independent tables, or is it a master/child setup?
If they are independent, I suggestion using one table and two sections, then depending on the section number return to correct cell.
For master/child, think about making a view that looks like a cell and have the master cells be the sections views and the children be the actually rows. You can make a section class that stores if the children are visible or not and in the numberOfRowsInSection method return the count if visible or 0 if not.
Just a couple ideas

IOS: How to create a Master View

I started a project with storyboards and it has a lot of views, each one of them has the same header (with an image, company name and two buttons). I want to do this once in the main View, and make it reusable for the other views. It’s like a master view with a header and footer. How could i do this?
This is my first project starting from cero and I want to make it as organized as possible. What are the best practices using storyboards and MVC?
Thanks in advance.
There is no special way of doing this in storyboard.
But you can do it easily by making custom UIView.
Make a sub class of UIView and pust all common design in it.
Place a UIView on top of every UIViewController.UIView.
Now set this top UIView's class with your custom class in identity inspector.
All the best.
A primitive way to do it in storyboard is to define there your master view, select all of its elements, copy them and paste them into the other views.
You can subclass UIViewController to add a header to its view, and UITableViewController to set it as the tableview header.

Reuse a custom UITableView in different UIViewControllers

I'm developing an iPhone app for iOS 5.1. I built a UIViewController which has a UITableView with a UISearchDisplayController and other views, all somehow related to the table. The UIVIewController is the delegate and the datasource of the table, and makes some customizations (graphical and business-related) to the table and search display controller. So far, everything was OK.
The problem is that now I want to put this same table (maybe with a different sublist of elements, of the same type) in different controllers (different screens of the app). I tried to add the tableView of my controller as a subview in other controllers, but it doesn't work. I tried to rebuild my table as a subclass of UIView (instead of UIViewController) and add it as a subview, but it neither worked (it loaded the view from a NIB file, but all its properties, including the IBOutlets, where nil or 0x000000).
After searching a lot, I didn't find any example of how to reuse a tableview in different controllers. Any hint? Any example? Should I build it as a UIViewController or as a UIView? Which class should be the delegate of the table and searchdisplay, keeping in mind that most of the logic I want to reuse is the code in the delegates?
Thank you all in advance
Wouldn't the easiest solution be to create your own Datasource class (maybe as a singleton) and then reuse this with the other controller? This way, your way of getting and managing the data is abstracted from the way of displaying it. Just the way it should be.
This is what MVC is all about.

how to reuse the same table view in different views?

In my app , i have UIScrollView in bottom of the page where it contains list of UIViews. i want display the same UITableView with same contents on all the views. Can any one help me out of this?
What you need in fact is the opposite : having one viewController managing your table AND a list of sub viewControllers managing your views. The the main controller load subView depending on the row selected (a bit like the tabviewcontroller does).

Adding a TableViewController to an existing table view

I've got a Table view and it's the child of the View ( see the IB hierarchy ) :
I want to start customizing the tableCells, so I assume I'll have to add a tableViewController . Where would I start in this instance ? the TableViewcontroller can't be a child of the MainViewController, can it ?
Cheers,
You would need to add a table view controller at the top level.
Then drag connections from the table to the controller of datasource and delegate
Then drag connections from the controller to the view for "view"
Then change the class of the controller you added (it's the last tab in the inspector) to your custom UITableViewController class (if you don't subclass it it does nothing)
Then make sure that you somehow retain the controller when the nib is loaded. Easiest way is with an IBOutlet in the file's owner.
Sorry if this is a bit general, but the specifics would consume pages. I don't recommend this approach unless you have some experience with this and know what most of the above means, in which case it's possible and sometimes desirable to do this, and good job if you do.
The easiest method, if you just want a simple table view, no special circumstances, is to use the File>New File>UITableViewController with xib for user interface, then go from there with the file it makes, but that isn't your question, so I have answered as best I can.
It would be easier to build a tableViewController class with a table inside that and add the tableViewController to the view instead of reverse-engineering it. The tableViewController will have all your delegate and datasource functions for your table fairly mapped out so you can move whatever you already have into those functions.
As for custom tableViewCells, here's a great tutorial from pragmaticstudio.com on how to do so. He walks you through all the aspects of building a tableViewController and also how to customize your cells in IB.