Insert UITableViewController into Other UIView - ios

I've got a UITableViewController that inserts custom UITableViewCells into a UITableView. Is there a way that I can put this UITableController/View combo into different views? I've got the behavior of the UITableView acting the way I want, I just want to use that UITableView in different UIViews (say a UIView with a UILabel above the UITableView and one with a UILabel below the UITableView).
I know I can make a UIViewController that has an embedded UITableView in it and have the UIViewController act as the UITableView's delegate, but then I would have code reuse (the UITableViewController logic would be in multiple UIViewControllers). Or am I looking at this problem the wrong way?
I want to somehow reuse the functionality of my UITableView in different UIViews.
Thanks

Yes, you can, simply instantiate (or get a reference to) the UITableViewController inside the UIViewController and call something like this:
[self.view addSubview:tableViewController.tableView];

Related

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.

UITableView inside a UITableView?

Generally:
Is it OK to add a UITableView as a subview of another UITableView? Or, should I create a UIView and add each UITableView to it?
Specifically:
For a ComposeTableViewController with a typeahead, like in the iPhone's native Mail app, which approach would you recommend and why?
Note: I prefer to construct things 100% programmatically (no Interface Builder).
Subclass UITableViewController.
Then, to show the typeahead results, create and add a resultsTableView as a subview of self.tableView positioned directly underneath the the cell (of self.tableView) with the typeahead text field.
The nice thing about this approach is that resultsTableView scrolls with self.tableView automatically.
But, is it OK to add a UITableView as a subview of another UITableView?
Subclass UIViewController.
Create and add tableView (custom property) as a subview of self.view.
Create and add resultsTableView also as a subview of self.view.
The annoying thing about this approach is that I have to reposition resultsTableView manually anytime self.tableView scrolls.
I think I'd prefer approach 1, but adding a UITableView as a subview of another UITableView just seems smelly to me.
TableViews cannot have subviews. You can try adding a tableview as the view of a TableViewCell, but then you have to ask yourself how it would scroll, if you tried scrolling in the subtableview would it scroll the child tableview or the parent tableview? It is much easier to present multiple tableviews within a view. This can be done easily by setting your custom viewcontroller as the datasource of both tableviews contained within its view and then comparing the tableview pointer that is sent as a parameter of the datasource method to the two tableview pointers that are IVars of your custom view controller.
Hold stuff like this in a container. Create a container view controller which only does typeahead. This container view controller will hold your main table view controller (and its table view) and it will hold the typeahead view controller (and its view).
It will keep the logic needed for the typeahead out of your main table view and as a bonus you might end up with a reusable typeahead container which could be applied to other views.
Yes it is good to go for adding UITableView in as a cell of another UITableView.
I had one issue and posted a question
Multiple Views as subviews
And requirement was like a group of controls with list of other controls. I thought that time that it will be messy if i'm going to add UITableView as a cell of UITableView.
What i found that... Boy !! it's how iOS meant to be. There is no any lag while doing this.
Tested for below functionalities:
Scrolls like a charm
Separate delegates called for each
Reordering of both UITableView cell
Swipe and remove cell
Runtime datasource update for both UITableView and reload both at the same time.
There is no any reason not to add subview UITableView.
You can do it but it is against the principle of MVC. You will be setting the delegate of a view to another view which is wrong.

Can a property of a UITableView be the UITableView Delegate and DataSource

I have a custom view that's a bit of a hack. Basically it's a UIView with tableView as it's property, with additional views in the tableView that need their own delegates. I need a viewController for the UITableView and can't make the UIView it's delegate according to this SO link Custom UIView as UITableView delegate and datasource?.
Can I make the UITableView have a property of UIViewController and set that UIViewController as the tableView's delegate?
In this case according to OOP, the UITableView has a UIViewController so technically, I could expect this to work. But, I am wondering if down the line somewhere this could create problems since the UITableView and UIViewController are coupled in this way.
You don't need a UIViewController for the UITableView - you just need an object or objects that implement the data source and a delegate protocols. As per the accepted answer on the question you linked to you can use a separate controller class to provide this.
The right answer depends a little on how the table is used with your UIView subclass.
If the table will always have the same content (Say a list of months) and there is no value in exposing or abstracting the properties then you can code the delegate and dataSource inside your UIView subclass or in an assistant class.
If the table content will vary depending on how the UIView is used (say a list of people where you don't know what the list is - friends, relatives, employees...) then it would make sense to simply expose the tableview's datasource (and delegate if necessary) properties via your UIView subclass

UITableView in UIView

I want to create a UITableView in UIView class. Can I? The UITable is nested in the UIView, so I figured, I should put the code of the the table in my custom UIView class, but on the other hand, usually the UIViewController is responsible for the table delegates. So what is best?
UITableView has a data source and a delegate which need to be implemented. These could be implemented separately in different objects or they could be implemented in the same object.
Many times, the UIViewController is used as the data source and delegate object but it's not required. Since you're adding the UITableView to a custom UIView it makes sense create a custom object to handle the data source and delegate.
It's fine to add a UITableView to a custom UIView which is not the UIViewController's view.

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