Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  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.

like image 979
ma11hew28 Avatar asked Jan 01 '26 09:01

ma11hew28


1 Answers

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.

like image 89
Nick Wilkerson Avatar answered Jan 03 '26 02:01

Nick Wilkerson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!