gpt4 book ai didi

ios - 在 collectionview 的第一行显示 3 张图像,在第二行显示 3 张图像

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

我将一个 Collection View 拖到我的 xib 上,将约束设置为前导、尾随、顶部和高度。

编码部分给出...

 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

}

This是我提到的链接。但它不起作用,我只看到 5 个宽度和高度相等的单元格,一个接一个。

我做错了什么......?

编辑 1:我还想像这样在左侧显示一个长单元格,在右侧显示 2 个小单元格...

我要对我的答案做哪些更改才能实现这一目标...? enter image description here

最佳答案

您的代码看起来很完美,但您可能忘记确认 FlowLayout 协议(protocol)。所以,您只需要在您的代码中确认以下协议(protocol),

UICollectionViewDelegateFlowLayout

希望以上方法能帮到你。

关于ios - 在 collectionview 的第一行显示 3 张图像,在第二行显示 3 张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56037680/

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