【问题标题】:custom cell and custom height for each每个自定义单元格和自定义高度
【发布时间】:2019-02-15 16:06:27
【问题描述】:

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

我订阅了uiСollectionviewВ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
        }
    }

【问题讨论】:

    标签: swift uicollectionview uicollectionviewcell cgrect


    【解决方案1】:
    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

    【讨论】:

      【解决方案2】:

      您是否尝试过将您的函数 func collectionView(_: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
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用高度数组并根据 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])
        
                }
        

        【讨论】:

        • 如何在其中一种情况下启用高度更改。我需要更改一些单元格的高度,其余的保持不变
        • 如果 indexPath.row == 1 重新调整高度 100 否则返回高度 200,您可以在 sizeForItemAt.ex 中决定。如果您想根据内容更好地布局单元格,则使用 Pinterest 布局(raywenderlich.com/…
        • 感谢您的链接。我不需要开关功能我需要为案例中的每个单元格指定一个单独的高度,案例 1 高度将是 100,案例 2,300 高度案例 3 高度 90 所有单元格同时显示。一件一件的遗憾,我还没能成功
        • 你能上传图片你想要什么样的结果。
        • 更新帖子。所有单元格都是自定义的,并在 switch 函数中按顺序输出
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        相关资源
        最近更新 更多