Swift dynamic tableview height - ios

I have a tableView which I want to size dynamically in height depending on how many cells are there (and to pull/push all views underneath it)
I've tried to archieve this by putting it in a StackView, but TableView content doesn't show unless I give it a fixed height constraint, which is opposite of what I'm trying to do.
Also tried this Swift dynamic tableview height based on content with no change, as the comment from OP said too.
Thanks in advance.

Set table frame or table height Constraint outlet with table content size means set the table view height constraint equal to its content size after the table view has done the loading.
tableHeightConstraint.constant = tableView.contentSize.height

Related

Dynamic height of two UITableViews embedded in a single view with Auto Layout

I`m trying to adjust the height of two tableviews embedded in a single viewController. .
This is how it is currently displayed
Current constraints
If possible, I want to adjust the height of each table to avoid the scrolling effect of the table (it is not possible when the total content of the tables is higher than the screen, in that case the height of each table must be the same )
I have manually changed the constraints to show you what behavior I want
Expected behavior 1
Expected behavior 2
I have no idea what constraints I have to modify and what part has to be programmatically
Here is a scenario:
Mapping Data to table.
Get tableview's content size
Set the constraint = height of the content size
Update constraint layout.
In your case, i would like to recommend to use 1 table view with 2 sections.
Hope this help.
The best solution would be not using tableView for the first section as its not a complicated content to be displayed on tableView. Even if you want to use tableView, try this.
Take height constraint of the top tableView. Take outlet of the height constraint in your controller. Then just after you are reloading the tableView, assign the contentSize.height to the constraint and call self.view.layoutIfNeeded(). This will change the height of the tableView to be same as content.
You can do same with bottom tableView.

Self-sizing UITableView embedded in UICollectionViewCell

For an iOS application I'm working on the following layout:
I'm trying to achieve this by embedding a tableview inside a collection view cell.
The height of the individual collectionView cells is dynamic (by setting the layouts' estimatedItemSize and using auto layout for the cells). The problem which I encountered is that I can't get the embedded tableView to size dynamically according to the given data.
Is there any way I can update the size of the tableView in the cell dynamically
For collection view or table view, auto-sizing will only work if you have provided enough constraint to calculate it's CGRect. In your case, you have table view inside collection view and table view's height can be anything as it can scroll the content.
Try to give table view height constraint then change constraint's value to table view's contentsize.height, Then it might work.
Maybe consider using UIStackView.

swift iOS full height table inside a table cell

Goal
I'm trying to create a table where each cell has a table inside of it. These inner tables are not scrollable and need to be full height based on their content size.
Code
Click here to see my repository with an example of what I'm trying to accomplish.
ViewController
Displays a table with a hard-coded array of food groups
Each table cell is a FoodGroupCell
FoodGroupCell
XIB with a label and a table (has a corresponding swift file)
Each table cell is a FoodItemCell
Has an #IBOutlet for the table's height constraint
Uses a KVO on contentSize
updates the height constraint to the table's content size
calls updateConstraints and layoutIfNeeded on the table cell
FoodItemCell
XIB with a label
Label has a height of 200
Problems
If I have a height constraint and no bottom constraint on the inner table the cells do not display properly.
If I have a height constraint and a bottom constraint on the inner table the cells appear, but do not adjust their size based on the label's 200 height.
This is obviously not ideal having a bottom constraint and a height at the same time. It does not seem necessary and xcode throws a bunch of warnings because of it.
Can anyone shed any light on my issue? Any help would be greatly appreciated.
Update
The problem was that I needed to update the constraint priorities, so that the height and bottom constraint could work together. I have updated my repository with the changes.
You may be able to work around the constraint issue by adjusting the priority of one of the two constraints so that it can adjust itself without being blocked by the constraints all having top priority.

UITableView embedded in view not sizing accordingly

I am trying to add table view that is contained into a view, this view has a width constraint and the height of the view will be dictated by the content height of the table view. However, I am unable to get this to work.
My attempt:
I create a view with a leading, top and width constraint. Then I insert a table view inside this view, this table view will have a leading, top, bottom and trailing constraint to the view (it's superview). I also set the row height and the estimate. When running the code, numberOfRowsInSection gets called but cellForRowAt does not, my guess is because the height didn't solve properly and the table has a height of 0. How can I fix this?
Thanks.
luis,
UITableView inherits from UIScrollView. So You cant expect the tableView to provide the height for its superView. In fact its vice versa.
TableView's frame will be decided by the superView's frame where as the tableView's contentSize will be decided by the data that you have provided.
So your best bets
Solution 1:
Add a height constraint to the View (SuperView) and create a IBOutlet to it in your VC.
Set the height for each cell in tableView lets say as 50.
Once you get the data to show in tableView before calling reloadData() on tableView set the constant of View's height constraint as
viewsHeightConstraint.constant = (numberOfRows) * 50;
yourView.layoutIfNeeded()
Solution 2:
If you are planning to use automatic height for cell then set the height of view based on the ContentSize of tableView. But to do that you have to get the data first, reload the tableView with the data and once done now get the content size of TableView and set the frame of your view accordingly.

Set dynamic Height of view according to height of tableView

I want to create a view like this. For that I took one view. Inside it, label(Specification) and then for data of specification I took one tableView. Data for specification could be 2,5,7 anything. Means I am getting array of specification, which could have any no. of elements. Now I want to set height of this view according to height of tableView and after this specification block I have another block which has y position according to specification view. How can I do this?
This is the image of output what I am getting. I don't want this scroll in tableView, instead of that I want it show with its maximum height in view.
You can set relation of the height constraint of tableView as <=. So that it will automatically adjust the height of the tableView, according to the data.
Change the UITableView height after loading the content in table using GCD.
Inner view of UITableView is based on ContentSize and outer view is based on frame size so just change frame size same as ContentSize. Outer frame size depend on super view frame size but inner view size is depend on content.
Follow the selected answer in Resizing UITableView to fit content
Hope it will help you.

Resources