Swift 自定义单元格和每个单元格的自定义高度

Swift 自定义单元格和每个单元格的自定义高度,swift,uicollectionview,uicollectionviewcell,cgrect,Swift,Uicollectionview,Uicollectionviewcell,Cgrect,我通过开关功能输出4个自定义单元格,现在我需要为每个单元格输出动态高度值或固定高度值。我喜欢两者,因为它们的高度总是固定不变的 我订阅了uiСcollectionview b elegateFlowLayout协议,并找到了如何更改所有单元格的高度 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionView

我通过开关功能输出4个自定义单元格,现在我需要为每个单元格输出动态高度值或固定高度值。我喜欢两者,因为它们的高度总是固定不变的

我订阅了
uiСcollectionview b elegateFlowLayout
协议,并找到了如何更改所有单元格的高度

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
        }
    }

您可以使用高度数组并基于indexPath检索单元格高度

let heightArray = [90,400,100,70]

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

                return CGSize(width: 357, height: heightArray[indexPath.row])

        }
完全解决了这个问题 别忘了添加

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
}
p、 s.如果您在“
collectionView.delegate=self
”中出现错误,可能是因为您在
viewController
中工作,而不是在collectionViewController中工作


您需要添加
IBOutlet

您是否尝试过将函数
func collectionView(uu:layout:sizeForItemAt:)
更改为类似的内容

let cellHeights = [ 90, 400, 100, 70 ]

// width and height for cell in collectionView
func collectionView( _ view: UICollectionView, 
                     layout collectionViewLayout: UICollectionViewLayout,
                     sizeForItemAt indexPath: IndexPath ) -> CGSize 
{
    return CGSize( 
        width: view.bounds.size.width, 
        height: cellHeights[indexPath.row] 
    )
}
此外,将
collectionView.dequeueReusableCell(withReuseIdentifier:for:)
的结果强制转换为
UICollectionViewCell
子类在代码中没有任何效果

我可能会将其更改为:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{
    // map row numbers to cell reuse identifiers...   
    let ids = [ 
        0: "secondCell", 
        1: "thirdCell", 
        2: "fourthCell" 
    ]

    // look up the reuse identifier for the row we want; if none found (subscript call returns `nil`), use `"imageCell"`
    let reuseID = ids[indexPath.row] ?? "imageCell"
    let cell = collectionView.dequeueReusableCell( withReuseIdentifier: reuseID, for: indexPath)

    return cell
}

如何在其中一种情况下启用高度更改。我需要更改某些单元格的高度,保持其余单元格不变。如果indexPath.row==1,则可以在sizeForItemAt.ex中决定是否重新调整高度100,否则返回高度200。如果要基于内容布局单元格,最好使用Pinterest layout(),感谢链接。我不需要为案例中的每个单元格设置一个单独的高度所需的开关功能。案例1的高度为100。案例2的高度为300。案例3的高度为90。所有单元格同时显示。一个接一个不幸的是,我没能成功yetCan你上传图片你想要什么样的结果。更新帖子。所有单元格都是自定义的,并在开关功能中按顺序输出
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{
    // map row numbers to cell reuse identifiers...   
    let ids = [ 
        0: "secondCell", 
        1: "thirdCell", 
        2: "fourthCell" 
    ]

    // look up the reuse identifier for the row we want; if none found (subscript call returns `nil`), use `"imageCell"`
    let reuseID = ids[indexPath.row] ?? "imageCell"
    let cell = collectionView.dequeueReusableCell( withReuseIdentifier: reuseID, for: indexPath)

    return cell
}