Я вывел 4 кастомных ячейки через функцию switch
теперь мне нужно для каждой из вывести либо динамическое значение высоты, либо фиксированное. Мне подходит любой из вариантов, так как их высота всегда будет статична.
я подписался под протокол UICollectionViewDelegateFlowLayout
и нашел как изменить высоту всех ячеек
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = CGSize(width: 357, height: 600)
return size
чтоб переопределить высоту отдельных ячеек я использую функцию
cell.frame = CGRect(cell.frame.origin.x, cell.frame.origin.y, width: 100.0, height: 100.0)
но она не работает, так как "Expression type '@lvalue CGRect' is ambiguous without more context"
как мне поступить в таком случае? какую функцию использовать?
полный код ниже
// 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
}
}
Еще раз уточняю есть три case + default значение. Все кейсы отображаются ячейки одновременно, последовательно
Для каждой ячейки мне нужно вывести кастомную высоту, не затрагивая высоту других.
Например
case 1 - высота 100;
case 2 - высота 500;
case 3 - высота 70;
default - высота 80;