3

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

  1. 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?

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

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • I've been wondering... Why would you want to put a TableView inside a TableView? Not that anything's wrong with it but I'm just really curious about why you would use that... – Steve Sahayadarlin Nov 04 '14 at 03:35

4 Answers4

1

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.

Nick Wilkerson
  • 353
  • 2
  • 17
  • Most views can have subviews. UITableViews definitely CAN have subviews. As for figuring out which table to scroll, you can override the scroll view's scrolling delegate methods to redirect certain scrolls if you want. But the rest of your points are good - generally it is better to find a different way. – Daniel Brim Nov 03 '14 at 23:45
0

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.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
0

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:

  1. Scrolls like a charm
  2. Separate delegates called for each
  3. Reordering of both UITableView cell
  4. Swipe and remove cell
  5. Runtime datasource update for both UITableView and reload both at the same time.

There is no any reason not to add subview UITableView.

Community
  • 1
  • 1
Kampai
  • 22,848
  • 21
  • 95
  • 95
0

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.

Sumit Oberoi
  • 3,455
  • 2
  • 18
  • 18