【问题标题】:Data not displaying correctly in CollectionView cell?数据在 CollectionView 单元格中未正确显示?
【发布时间】:2021-06-26 00:43:12
【问题描述】:

我正在尝试制作一个显示水晶图像的程序。编写代码并格式化 StoryBoard 后,当我运行应用程序时,图像很小并且没有显示任何标签。我有限制。

代码如下:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let imageArray = [UIImage(named: "1"),UIImage(named: "2"),UIImage(named: "3")]
    let nameArray = ["Rose Quartz", "Clear Quartz", "Clear Quartz 2"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainCollectionViewCell", for: indexPath) as! MainCollectionViewCell
        
        cell.crystalPhotoImageView.image = imageArray[indexPath.row]
        cell.crystalNameLabel.text! = nameArray[indexPath.row]
        
        return cell
    }
    
}

【问题讨论】:

  • 我现在在单元格的大小检查器中取消选择了“内容视图”,现在一切都变得巨大了,哈哈。还在折腾……
  • 你是否在可选的 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize中设置了collectionView单元格的大小
  • 在 UICollectionView 的 Size Inspector 中将“Estimate Size”更改为“None”并修复了它!

标签: ios swift iphone xcode uicollectionview


【解决方案1】:

你需要实现确认类有 UICollectionViewDelegateFlowLayout 并且你需要实现这个功能n

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

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多