gpt4 book ai didi

ios - Collection View 单元格不会改变大小

转载 作者:行者123 更新时间:2023-12-01 18:04:02 25 4
gpt4 key购买 nike

我正在尝试创建一个带有 Collection View 的应用程序。无论我做什么,我都无法改变它的大小。在 View Controller 中,它看起来是 larger比程序为 ran 时(下面的照片)。 ImageView 与单元格大小相同。我也尝试过使用

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: screenWidth/3, height: screenWidth/3)
}

但没有任何改变。

这是 View Controller 代码:
import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

let array = ["1,", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

override func viewDidLoad() {
super.viewDidLoad()
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell
cell.myImageView.image = UIImage(named: array[indexPath.row] + ".jpg")

return cell
}

}

myCell 只是一个 UICollectionViewCell 类,带有一个到 imageview 的导出。

最佳答案

要调用 sizeForItemAt 方法,请确保您的类同时采用 UICollectionViewDelegate UICollectionViewDelegateFlowLayout 协议(protocol)。

关于ios - Collection View 单元格不会改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58907991/

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