How do i set UICollectionView height automatically inside UITableView - ios

I want to make my UICollectionView height automatically because the data inside the collection view cell is dynamic.
If i'm delegate the table view as tableView.rowHeight = UITableViewAutomaticDimension, it'll not call the cellForItemAtIndex. But if i delegate the table view height, it'll call func cellForItemAtIndex, but the size'll be static.
How to make that height automatically?

Related

How to populate Dynamic UITableview Inside dynamic UITableviewCell in swift?

I need to implement the dynamic UITableView inside dynamic UITableviewcell like the below screenshot. Also, all values are dynamic which is coming from the server.
yes exactly you need inner tableview to do this.
first you have main tableview with card layout cell this should be dynamic cell height.
in this cell you have topview, inner tableview, bottom view all this view you can take fix height as your layout i see e.g top view is 80 height, bottom view is 80 height and tableview has dynamic height based on number of cell like cell height is 100 and you have total 3 cell so table height is 300 you need to make outlet of tableview height and update it programmatically.
hope this helps you!

Calculate height of tableView height inside the collection view cell

I used tableView inside the collectionView cell.My question is how to calculate the tableView height and set that height in collectionview cell.
NB: The table view height will be dynamic.
Please help me.
Since UITableView is a subclass of UIScrollView you can get its height by calling tableView.contentSize which will give you width and height of the table view's content
You can put KVO(observer) on contentSize of tableview, so whenever contentSize of tableview will get updated, you will get notified, at that time you can modify the height of collection view cell.

Auto set height of table view based on UICollectionView inside it

I have a UITableView with cells that have inside a UICollectionView.
Now all works great except of UICollectionViewCell scrolling i want that the height of table view cell follow the Collection view cell content.
In simple word i want scrolling in table view but not inside single cell i need to display all contents of UIcollectionViewCell without scrolling inside it.
Thanks
You have to calculate the collectionviews' heights and set a height constraint on each of them. then add top and bottom constraints from the collectionviews to their superviews (the tableviewcells / their contentviews) and let autolayout do its work.
don't forget
tableView.estimatedRowHeight = 44 // or something similar
tableView.rowHeight = UITableViewAutomaticDimension
in the controller's viewDidLoad to make the automatic row height calculation work.

Dynamically changing a cell's height that contains a collection view

I have a table view with various cells. They are all set to resize using
tableView.estimatedRowHeight = 45.0
tableView.rowHeight = UITableViewAutomaticDimension
For example if the cell is a label type and the label contains multiple lines of text, the label will expand and cause the cells height to increase. I have just created a new type of cell with a collection view inside it. It works great however the problem is that when more cells are added the collection view it's frame doesn't grow in size and thus the cell's height doesn't expand. Instead the collection view just becomes scrollable. Is there a way to make the collection view frame expand as more cells are added instead of making it scrollable? Any pointers on this would be really appreciated! Thanks!
You can set a constraint to the collection view and then resize it by code.
#IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
let height = cells.count * a //a = height of the cells within the collection view
collectionViewHeightConstraint.constant = CGFloat(height)

Setting Row Height At cellForRowAtIndexPath For Different Row Heights

Tableview has three types of custom cells with dynamic height. I want to set the row height using like tableView.rowHeight = 100 in cellForRowAtIndex() method.
Is this appropriate?
1.Define auto layout constraints for your prototype cell
2.Specify the estimatedRowHeight of your table view
3.Set the rowHeight of your table view to UITableViewAutomaticDimension
Do these code in viewDidLoad
tableView.estimatedRowHeight = 68.0(actual height of your prototype cell)
tableView.rowHeight = UITableViewAutomaticDimens
No, that is not how you need to do it. Never make any attempt to set any table attributes inside the cellForRowAtIndexPath method. Just create and return a cell.
If your cells have varying heights, simply implement the heightForRowAtIndexPath method and return the appropriate height for a given indexPath.
If all rows have the same height, then set the table view's rowHeight property in viewDidLoad or other appropriate place.

Resources