gpt4 book ai didi

iphone - 如何在gridview中调整单元格大小

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

我有一个看起来像这样的网格列表

class ListViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{

private var collection: ACollection?
private var collectionViewLayout:UICollectionViewFlowLayout?


override func loadView() {
super.loadView()

collectionViewLayout = UICollectionViewFlowLayout()
collectionViewLayout?.itemSize.width = 128
collectionViewLayout?.itemSize.height = 227
view = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionViewLayout!)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let someObj: SomeObj = collection!.getObject(index: indexPath.item)
let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(UICollectionViewCell.self), for: indexPath as IndexPath)
let cellView: UIImageView = UIImageView(image: object.image)

cellView.frame = cell.bounds
cell.contentView.addSubview(cellView)
return cell
}

我希望我的单元格具有不同的纵横比,尽管这取决于 someObj 中的值。如何在不重叠的情况下调整单元格的大小?我试过了

cellView.frame = CGRect(x:0,y:0,width:100,height:300)
cellView.frame =cell.bounds

但细胞重叠。

最佳答案

也许我会详细说明这个问题。

首先,如果您实现了 collectionView(_:layout:sizeForItemAt:) 方法,则无需指定以下内容:

collectionViewLayout?.itemSize.width = 128
collectionViewLayout?.itemSize.height = 227

这是每个单元格的默认大小。

其次,您在每个默认单元格顶部看到的重叠可能是您在重复使用的单元格上添加的 cellView(UIImageView)。问题是,每次委托(delegate)请求单元格时,您都试图在当前单元格上添加 UIImageView 。您需要检查 UIImageView 是否已经存在,因为整个单元格可能已被重复使用,但仍然具有之前添加的 ImageView 。

关于iphone - 如何在gridview中调整单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42335352/

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