I brought 4 custom cells through the switch function now I need to output either a dynamic height value or a fixed one for each of them. Any option suits me, as their height will always be static.

I signed up for the UICollectionViewDelegateFlowLayout protocol and found how to change the height of all the cells

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let size = CGSize(width: 357, height: 600) return size 

To override the height of individual cells I use the function

 cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0) 

but it doesn’t work, since 'Expression type' @lvalue CGRect 'is ambiguous without more context "

What should I do in this case? what function to use?

full code below

 // main protocols UICollectionView extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { // width and height for cell in collectionView func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let size = CGSize(width: 357, height: 600) return size } //margins for cell in collectionView func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 40.0 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell: UICollectionViewCell! switch indexPath.row { case 1: cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as? secondCollectionViewCell cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0) case 2: cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath) as? thirdCollectionViewCell case 3: cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fourthCell", for: indexPath) as? fourthCollectionViewCell default: cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as? imageModelCollectionViewCell } return cell } } 

Once again, there are three case + default values. All cases are displayed cells simultaneously, sequentially

For each cell, I need to display the custom height without affecting the height of the others.

for example

 case 1 - высота 100; case 2 - высота 500; case 3 - высота 70; default - высота 80; 

    1 answer 1

    You should not use

     cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0) 

    Instead, you need to specify a specific size returned by the function sizeForItemAt, for example:

     extension ViewController : UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch indexPath.row { case 0: return CGSize(width: 100, height: 200) case 1: return CGSize(width: 100, height: 300) case 2: return CGSize(width: 100, height: 400) default: return CGSize(width: 100, height: 100) } } } 

    Remember to make sure you specify a delegate:

     override func viewDidLoad() { super.viewDidLoad() collectionView.delegate = self } 
    • when trying to add return for each case return CGSize (width: 100, height: 200) It displays the error Cannot convert return expression of the type 'CGSize' to return type 'UICollectionViewCell' - Showa
    • You have inserted the code not in the sizeForItemAt function, but in cellForItemAt. These are different functions. - Ivan Kramarchuk
    • Ivan, thank you. I think I understood what made a mistake. Could you show me with an example of my code, as a rule, do this? Thank you - Seva
    • You have it all right. Remove the cell.frame = CGRect line (cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0) and add my code. - Ivan Kramarchuk pm
    • Ivan, can you look at my new code under the footnote // update? I updated it in a post - Seva