How to create 2 steps expandable tableview Swift - ios

There is a main table view and inside each of the main table view cell has their own header table view. Then each of the header table view cell has their own sub-header table view. All the table rows don't have fixed counts according to the dynamic data. What I try to achieve is when a cell of the main table view is expanded, I should see the header table view cells unexpanded. After a header table view cell is expended, I should see the sub-header table view. I need a solution without using libraries.
I provided the storyboard UI based on what I want to achieve.

To deal with expandable cells (or cells inside cells effect) I recommend using UIStackView inside UITableViewCell and add inside needed views with data.
For example you can create UIView inside Xib file and load it inside needed UITableViewCell then add to UIStackView and fill with data.
Then you can simply hide/unhide elements inside UIStackView to archive expanding/unxpanding.
This way or other you should use 1 UITableView.

I once made a 1 step expandable Table View.
Here is what I did to achieve that.
Do not make different table views for each hierarchy. That will be difficult to manage. Instead, make one table view but change the type of cell you want to show. If the cell is top level cell or first level cell or the second level cell, change the design of each one that.
The data source will then have list of list of list to denote the 2 level expandable UITableView.
To open and close the table view, you can simply change the data source and reload or use beginUpdates and endUpdates

Related

How to make the cells inside of a tableview begin lower down?

I have a tableview. Right now the first cell will begin right at the top of the tableview, but I want it to start some number below the top of the tableview so that I can addSubview() in that spot without covering content in the first cell.
Below you can see what I want to achieve.
The rectangle in the center represents the cells, the line at the top represents the top most of the tableview.
Short answer: You don't. The contents of a table view belong to the table view. You should not try to add subviews to a table view.
As #rickramirr says, you either need to position your table view lower on the screen, or use a header view that contains the contents you want.
(If you decide to position your table view lower down, need to either not manage your table view with a UITableViewController, or have a UITableViewController as a child view controller of the screen's main view controller. A UITableViewController is kind of dumb, and only knows how to manage a table view and nothing else.)
I think that maybe you should add a custom header to your table view or put another view above your table view. Here is Apple docs to customize your header: https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections
All the views that you want to insert between one tableView cell and another must extend UITableViewCells too. You can create custom cells by defining a new xib file containing a UITableViewCell, whose content could be whatever view you want. Then, you must create a swift file that extends UITableViewCell associated with the xib file. Finally you need to record that cell into the table, either programmatically or by manually inserting it into the storyboard within the table. At that point you will decide its position using the UITableViewDelegate "cellForRowAt" method.

Can we add tableview to cell?

I want to customize my tableview. I want to add another tableview in cell. Can we add tableview in cell of another tableview?
Please give me some ideas.
You can embed a UITableView into another UITableView's cell, but due to potential issues with the embedded UIScrollViews it is not recommended.
If you feel like you need a table in a table, first consider if it is not possible to achieve the desired behavior by using sections and cells in the sections (a section in a table might represent the top tableView's cell, and cells in the given section would represent the embedded tableView's cells).
There is no problem with adding a UITableView into a a UITableViewCell, since both of them are UIViews its simply would be translated to adding a subview to a view...
However, keep in mind to consider the following issues:
1- From a user experience perspective, that would be a vertical scroll view inside another vertical scroll view (are you ok with that?).
2- For each cell -in the main container table view-, you would need to handle the table view delegates/datasources. For understanding the logic of how this could be done, you might want to check:
Is it possible to add UITableView within a UITableViewCell.
Putting a UICollectionView in a UITableViewCell in Swift: although it is about adding a UICollectionView inside the cell, it helps to gain the logic of how to achieve such a task.

Can I set grouped tableView section number in storyboard?

I dragged a tableView to my controller in storyboard. I set the type to grouped, but I don't know if I can set the tableView's section number in storyboard.
I can not see the tableview's content to Static Cells. If I do this it will report error.
You cannot do that in tableView because it does not support static cells.
However if you use TableViewController
you can create static cells and you can create your desired sections and as well as add section number too.
Table View controller gives a more flexibility than Table View alone.

How to tell apart prototype table view cells?

Say I have a table view with a single prototype cell in it that contains a stack view. I'd like each cell of the table view generated from the prototype cell to have different views in the stack view. So, I need to find a way to tell table view cells apart.
If the table view had static cells, I could just use the tag property of the cell and populate stack views easily. Though, using static cells is probably not the best approach since cells are almost identical to each other.
What can I do to populate stack view differently depending on the "row" of the table view cells?
That's precisely what you should be doing in your tableView:cellForRowAtIndexPath: method.

UITableViewCell is not available in XIB file in XCode 5

I have just encountered a problem today that I am unable to add a new table view cell to my UITableview.
What I did was I drag a table view cell and drop inside the UITableview area. The UITableview did not take the table view cell but rather the cell become an individual view of the main view.
Clearly see that table view cell is not included in Table View
So my question is, how to create a custom table view cell in UITableview in an XIB file in Xcode 5?
Thanks in advance!
If you have dynamic prototype cell will be added automatically. If not, you can choose how much cell you want to have in inspector on right side of screen.
And make sure when you put it into screen that is inside of your tableview.
I answer this question thinking you are using storyboard. First add UITableView.
Then you can see as this,
If you want to use static cell, select Table View & in Attributes Inspector , in content select Static Cells. I think for your question you should use Static Cells.

Resources