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;