Self-sizing UITableView embedded in UICollectionViewCell - ios

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.

Related

Can we make UIView height always resize with the height of UIStackView inside?

I have a content view (this content view I will use as a subclass of UICollectionViewCell content later) which has an UIStackView inside. Inside that stack view, I add some labels, view. And I calculate some conditions to make sure that when data of a label is empty, that label will be hidden and the stack view will auto change the height and vice versa, if they have data I'll show them and stack view update the height again.
Now I wanna know how can I make the content view height always follow the UIStackView height whenever it's changing.
If you already made your UIStackView to have a dynamic height following its children, then all you need is to add constraints between UIStackView and your content view and it will resize automatically as well.
Now, if you add that content view inside a UICollectionViewCell, then you must make sure to call UICollectionView.reloadData() whenever you change the content of your UIStackView. That way the collection view will recalculate the size and render the cell again. You also have the option to reload a single cell in the collection view if you have a way to determine its indexPath.
Note: Take care of CollectionViewFlowLayout and what size it dictates to the cell. It's recommended that you tell the flow layout the estimated height of your cell via layout.estimatedHeight.

Swift dynamic tableview height

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

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.

Auto Layout: Set height View depends on other height View

I need to implement AutoLayout in my layout. Below are some screenshots:
1.
2.
The case is :
I have a ShinobiDataGrid in View and a UITableView . First image is first condition, in first picture I want the view have height as height View Controller.
The second and third picture is when there are available TableView . I want the TableView always is at the bottom of View Controller and the View is have height depends of height Table View.
I read this, this and all of reference on that but it's not working for me.
Edit
I read some article about Auto Layout and I get this below:
1.
2.
But in first picture, the the UIView still have a distance with main View. I want it only have 3 or 4 point from bottom of main View like below:
Now, I have one constrain from UIView to the UITableView. I add one constrain from UIView to bottom main View with custom priority, constant and etc but it's not working.
1.I would suggest you to use UITableView with section header and footers , with no scroll. Add your views to UITableViewCell. Populate your UITableView with custom cells based on your requirements.
2.You can also use UIStackView for the same. Add all your views & table views to a vertical stack view in Interface Builder. Keep distribution as fill equally. First time hide your UITableView. Your view will cover complete then show / hide tableview when required and update stack view.
Constraint your views & table views with respect to stack view. UIStackView manages autolayout.
3.I would have used UITableViewController with static cells (with no scroll) with views and tableviews as subview of static UITableViewCells. First time in heightFoRowAtIndexPath pass complete height. When tableview will be visible. Reload tableview controller and pass height accordingly.
Instead of using UITableView inside cell. I would suggest using UIStackView as subview cell. Show your data in UIStackView.
First add scrollview.
Add the height constraint of First View equal to view controller.
Below to that, add the tableview, get the dynamic height and change according to it. You can see my answer here for this.

Can hard coded and self-sizing table cells be intermixed within a tableview?

I'm working on an app that will populate the tableview based on a web service response. So far the two content types I know I will be getting will be data shown in a textview, and also in a collection view. I've begun implementing Ash Furrow's AFCollectionView. The dynamic sizing for text areas is working, however the collection view is scrolling within a cell height that is even less than my estimatedRowHeight (210.0).
Because the collection view table cell is not correctly resizing is it possible to explicitly set the height for the collectionView cell(s), and let any of the text based cells remain dynamic?
Your self sizing cells use auto-layout to determine what height to be, and that's based on the intrinsic content size of the subviews. But, that's not the only way auto-layout can specify the height of a view. You can add a static height constraint to your cells which contain a collection view.

Resources