gpt4 book ai didi

ios - 使 UICollectionViewCell 与 UICollectionView 的宽度相同

转载 作者:行者123 更新时间:2023-11-28 11:25:17 25 4
gpt4 key购买 nike

我写这个问题只是为了引用,并帮助遇到我遇到的同样问题的任何人。 SO 和谷歌搜索中的许多答案都没有提供完整的解决方案。所以在这里我回答我自己的问题

最佳答案

这是可以正常工作并正确设置宽度和高度的代码

import UIKit

class MyViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

@IBOutlet weak var myCollectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()

myCollectionView.delegate = self
myCollectionView.reloadData()
}

//Other methods here

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.size.width
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) //Put your Cell Identifier istead of Cell
let height = cell.contentView.frame.height

return CGSize(width: width, height: height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return .zero
}
}

关于ios - 使 UICollectionViewCell 与 UICollectionView 的宽度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364108/

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