Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple UITableViews visible at once in one UIViewController

I have seen questions asked about mutliple UITableViews in one view but they all have only one table visible at a time. I am after something different please.

In my head I want four UITableViews visible in one UIScrollView inside one UIView. The four tables will be visible and populated at once. They will have a dynamic number of rows each so the scroll view will allow users to scroll off of the page to see rows that do not fit.

The tables would be two side by side and then below them the next two side by side so that you end up with a 2x2 square.

I can (sort of) wrap my head around how to code this in the controllers etc. but I cannot figure out how to organise the hierarchi. I have tried using the storeboard to layout the tables inside the view but 9 out of 10 attempts to drop controls in fail as I am obviously not fully understanding this.

Do I need to generate the UITableViews in the UIViews implementation file and add them as objects to the UIView? Or can I use the Storyboard?

Could someone please explain how the hierarchi of objects would be structured?

In my head it would be: UIViewController

-> UiView

---> UIScrollView

------> UITableView

------> UITableView

------> UITableView

------> UITableView

But trying this in Storyboard doesn't work. I assume each UITableView will want its own UITableViewController and what I have read in other posts I would likey need to do this connecting in the UIViewController implementation file.

Any help is appreciated.

like image 835
Craig Moore Avatar asked Feb 09 '26 18:02

Craig Moore


2 Answers

I think you might try to drag UITableViewController into your view Controller, at least I don't have that problem to add 4 table view into a scroll view.

here is how i added it

1.> Drag the scroll view control into view controller

enter image description here

enter image description here

Your view controller should look like this:

enter image description here

2.> Drag the table view control into the scroll view, and set the size and position of that table view

enter image description here

enter image description here

Your view controller should look like this:

enter image description here

3.> Then drag all the rest 3 table views onto Scroll view

enter image description here enter image description here

But i would like to suggest a couple of things in your case

  1. no using that much table view in the same view controller, it's a chaos in your codes to maintain all them. There are always better options than 4 table view, maybe consider collection view. or even separate the use flow.

  2. If i were you, i won't use table view inside Scroll view, they are like scroll view inside scroll view, if you don't design the interaction very very well, they become extremely hard to use.

  3. If you still want to use four table view in the same view controller after all, you want to pay extra attentions on your table view datasource and delegate. very carefully handle all the cases.

Hope that helps you.

like image 78
Xu Yin Avatar answered Feb 12 '26 16:02

Xu Yin


Tableviews are very customized scrollviews. I wouldn't put 4 of them on a scrollview, this wouldn't be very intuitive for the user as your finger would scroll the view in many ways depending on where exactly it touches the screen.

Instead, try having your 4 tableviews in a 2x2 pattern directly onto a simple UIView. This can be done inside the Storyboard.

As for filling up and using them, you have 2 ways :

A) Your UIViewController is the delegate and datasource of each of the 4 tableviews. When executing, you perform a switch on the first parameter (the tableview that called you) to determine the appropriate course of action.

B) You create 4 classes that each handle a single tableview, instanciate 4 objects inside your UIViewController and assign the UITableviews' delegate and datasource properties to these objects.

All technicality aside, 4 tableviews in a single screen is pretty crowded. Your design will definitely not fly on a iPhone, so I'm assuming iPad only. Even then, you should make sure that everything is visually appealing and the purpose of each control is clear. It's up to you, but I'd hate to see you work hard on an application only to see your efforts wasted because your visual design doesn't appeal to your users.

like image 20
Jonathan M Avatar answered Feb 12 '26 15:02

Jonathan M



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!