UICollectionView, fix column with dynamic cell height - ios

I searched a lot about collectionView, but I did find a simple one, as the below,
My collectionView has 2 columns when Compact, 3 columns when
Regular,
There are many cells, all the cells' height are the
same, but not a fix number, which depends on some contents, but each
cell's height are equal.

Related

Dynamically size both UICollectionViewCell and UICollectionView, maybe UITableViewCell

I'm building something as shown in the image below:
I'll try to explain a bit more what my goal is. I have a UITableView. One UITableViewCell (1) in here will contain a title (left out in this image) and a UICollectionView (2) (same width as tableview/screen). This collectionview uses a subclass of UICollectionViewLayout to make it horizontally scrollable, evenly spacing the items.
Each UICollectionViewCell (3) in here contain a title and description, both with dynamic heights. Also, this cell contains a new collectionview (4). This new collectionview contains a number of items (5). In this most inner collectionview a row can contain a maximum of 5 items.
Currently I have a tableviewcell (1) with horizontally scrollable collectionview (2), putting the items (3) next to each other. I also have the most inner collectionview (4) putting the items (5) in the correct way.
The difficulty I'm facing now is:
To dynamically size the height of collectionview (4), depending on how many rows for the items are needed, so depending on the number of items.
To dynamically size the height of collectionviewcell (3), depending on the height of the title, description and collectionview (4).
If possible, to dynamically size the height of the collectionview (2) and the tableviewcell (1) containing this, as high as the biggest cell (3).
I hope someone would have an idea how to do this, if it is even possible. I do hope so, while keeping the code 'easy' to follow.
I can do a lot with constraints / autolayout, but this I can't get my head around.

How to get the height of UITableView that is bigger than the screen (iOS)

I have a tableView that is larger than the screen height, and has dynamically sized cells (UITableViewAutomaticDimension). I'm getting the height of the tableview the following way:
self.tableView.reloadData()
self.tableView.layoutIfNeeded()
let tableHeight = self.tableView.contentSize.height
However, this height isnt always correct, especially for long lists with cells of drastically different sizes. It seems to just take the table's estimated height and multiplies it by the number of rows.
How do you get the exact height of a tableView that has different cells?
i had to create a method to detect when the bottom of the tableview was hit and reload the tableview, it was the only solution.
rule of thumb, try not to nest dynamically sized tableviews inside a scrollview.

CollectionView different sized cell origin.y is different from the cell beside it.

Presently my output is this but I need the layout to be little more organized.
I want the frame.origin.y for 1 and 2 cells to be the same as cell 0.
I have made section insets and min spacing all 0.
To brief it, I want the 1,2,3,4 cells to be adjusted in the first row.
Is there any way I can achieve this?

In tableview i have 4 static cells. How do i make row height automatically calculated to fit the label inside and one manually

So i have four sections and one cell in each one of the sections in my static tableview. One of them is displaying a picture and for that one i manually calculate the ration and multiply it by the width and that way i get the correct height. I do that using the "heightForRowAtIndexPath".
I also have three other cells and each one has a label in it. The labels content is different each time since i'm segueing to it from a different cell so sometimes the cell should be big enough for 1 row of text and sometimes for more. How do i calculate/set that to happen automatically?
Also in my storyboard i have the constrains for the label set to be 8 point away from the right left and top, thats all the constrains that are on the label. I already tried setting the tableView.rowHeight to UITableViewAutomaticDimensions but that doesn't do anything. The rows just stay at the same height as they were set in the "heightForRowAtIndexPath" or the same as in the storyboard if that function is not implemented. Been trying to solve that for probably more then an hour now and still cant figure it out. Thanks for the help. Also i'm doing all of this in swift.
If you implement heightForRowAtIndexPath it will also override any value for the table view's rowHeight. So, in heightForRowAtIndexPath return UITableViewAutomaticDimension, and do the same in estimatedHeightForRowAtIndexPath too.
As long as your cell has a solid Auto Layout configuration, that's all it takes to make auto-sizing cells work.

Display one row in UICollectionView - Swift

I'm trying to figure out how to limit a UICollectionView to display a horizontal scrollable list in a single row.
Any Ideas?
I just used something that is working for me:
Set the Scroll direction to Horizontal.
Set the size of your Cells using sizeForItemAt indexPath method.
Set a constraint to your CollectionView making its height equal to (or a bit greater than) the Cell's height which you specified above.
The logic behind it:
When the height of CollectionView is equal to the height of it's items and it is set to horizontal scroll, then it can only show them in one row. I said "or a bit greater than cell's height" because if you set the height of your CollectionView twice bigger than cell's height then it can fit 2 rows of cells and it will show them in 2 rows instead of one.

Resources