gpt4 book ai didi

swift - 每个自定义单元格和自定义高度

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:25 25 4
gpt4 key购买 nike

我通过 switch 函数输出 4 个自定义单元格,现在我需要为每个单元格输出动态高度值或固定高度值。我喜欢两者,因为它们的高度始终是静态的。

我订阅了 uiСollectionviewВelegateFlowLayout 协议(protocol)并找到了如何更改所有单元格的高度

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)

但它不起作用,“表达式类型‘@lvalue CGRect’在没有更多上下文的情况下不明确”

遇到这种情况我该怎么办?使用什么功能?

完整代码如下

// 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? cellThirdCollectionViewCell

case 3:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fourthCell", for: indexPath)
as? fourthCollectionViewCell

default:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath)
as? imageModelCollectionViewCell
}
return cell
}
}

enter image description here

最佳答案

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

var cell: UICollectionViewCell!

switch indexPath.row {
case 1:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath)
as? firstCollectionViewCell


case 2:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath)
as? secondCollectionViewCell

case 3:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath)
as? thirdCollectionViewCell

default:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath)
as? imageModelCollectionViewCell
}
return cell
}
}

彻底解决问题不要忘记添加

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
}

附注如果您收到“collectionView.delegate = self”错误,可能是因为您在 viewController 而不是 collectionViewController

中工作

需要添加IBOutlet

关于swift - 每个自定义单元格和自定义高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54713017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com