gpt4 book ai didi

ios - 无法在左侧显示长长的 collectionviewcell,在右侧显示小单元格

转载 作者:行者123 更新时间:2023-11-28 13:40:57 29 4
gpt4 key购买 nike

我有一个 UICollectionView,它在顶部显示 2 个单元格,在底部显示 3 个单元格,如

中所示

This link

override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 5
layout.minimumInteritemSpacing = 5

collectionview2.delegate = self
collectionview2.dataSource = self
collectionview2.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "ident")

}
}


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


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
switch indexPath.item {
case 0,1:
return CGSize(width: (UIScreen.main.bounds.width - 16) / 2, height: (UIScreen.main.bounds.width - 16) / 2)
default:
return CGSize(width: (UIScreen.main.bounds.width - 32) / 3, height: (UIScreen.main.bounds.width) / 3)
}
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ident", for: indexPath)
cell.backgroundColor = .red
return cell

}

但是我想像这样在左边显示一个长单元格,在右边显示 2 个小单元格......

enter image description here

那么我需要在我的答案中做出哪些改变......?

EDIT 1 这是最新的输出...

代码在sizeForItemAt..

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
switch indexPath.item {
case 0,3:
return CGSize(width: (UIScreen.main.bounds.width - 16) / 2, height: (UIScreen.main.bounds.width - 16) / 2)
case 1,2,4:
return CGSize(width: (UIScreen.main.bounds.width - 32) / 3, height: (UIScreen.main.bounds.width) / 3)
default:
return CGSize(width: (UIScreen.main.bounds.width - 32) / 3, height: (UIScreen.main.bounds.width) / 3)
}

}

输出:

enter image description here

最佳答案

检查这可能对你有帮助:)

DynamicHeightForCollectionView

Result Image

关于ios - 无法在左侧显示长长的 collectionviewcell,在右侧显示小单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039426/

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