Peter R
Peter R

Reputation: 3516

xcode - Swift - How to get a dynamic table view so that each cell can expand and show more information when selected

So I can find some basic info about doing something similar with static cells. I can also find info about changing the cell height, but what I really want is a part of the cell that was previously hidden/covered, to appear when the cell is selected.

________________________________
|                               |
|       This displays normally  |
|                               |
________________________________
|                               |
|       This displays normally  |
|                               |
________________________________
|                               |
|       This displays normally  |
|                               |
________________________________

If it normally looks like this, when the second cell is selected, it should then look like this:

________________________________
|                               |
|       This displays normally  |
|                               |
________________________________
|                               |
|       This displays normally  |
|                               |
________________________________
|                               |
|             This              |
|             only              |
|           displays            |
|             when              |
|             cell (above)      |
|              is               |
|           selected            |
________________________________
|                               |
|       This displays normally  |
|                               |
________________________________

What are some of the ways to do this? should I insert a cell below when it's selected? Or should I treat both as parts of one cell? Thanks!

Upvotes: 0

Views: 158

Answers (3)

Jaydeep Virani
Jaydeep Virani

Reputation: 181

You can take another table within cell. And insert flag value in each dictionary of the array. Then after u are changing this flag value on tableview didselectrow or button click, whatever u used for selection. After changing this value , one more condition written in tableview cellforrow to related flag which is inserted in dictonary of array. Then after, give inner tableview height based on content size in cell for row. One more thing that you done that take height outlet of inner tableview on main cell, so that we are define inner tablview heights in cellforrow method. Every thing is depend upon your flag which is inseted into each dictionary of array so manage array as per your requirement. I have done already into my many project if you can't undestand then you can contact me.

Upvotes: 1

UfoXp
UfoXp

Reputation: 619

Inserting helper cells is too much hassle in such simple case and expand animation will not look great.

If you do not use dynamically measured cells (as in basing cell height on constraints, not table delegate calls) then you do this by calling beginUpdates on UITableView, returning different height from tableView(heightForRowAt:) in delegate for that specific row and calling endUpdates on table. I’ve used this method many times over with much success. Instead of using begin/end updates you could also call reloadRows(at:with:) which might or might not look better depending on your taste and code preferences.

I have not done this with dynamicaly measured cells but I assume you should be able to add a height constraint on main view in cell (I’m assuming it has already constraints for filling in cell). Then when you want to expand cell you call beginUpdates on UITableView, increase height constraint (you get cell instance by cellForRow(at:) method), call layoutIfNeeded on cell and call endUpdates on table.

Remember that you have to restore expanded state when you scroll table view back and forth so you need to remember which cells are expanded somehow. This is best done using “view models” e.g. array with booleans stating whether cell is expanded and using that to return proper cell height in delegate or change constraints in cell setup.

Upvotes: 0

Steve
Steve

Reputation: 951

I think your best bet will be to use automatic dimensions for the tableview row height and setup the cell with the ability to show a small view by default but also be capable of showing an expanded view. When the expansion occurs it should reload the cell to auto calculate the height of the expanded view.

Upvotes: 0

Related Questions