gpt4 book ai didi

swift - Collection View 不返回单元格(Swift 3)

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

好吧,我在 UIView 中添加了一个 UICollectionView 并添加了一个项目,但我似乎无法弄清楚为什么该项目没有显示在 Collection View 中。这是我到目前为止所拥有的:

override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.delegate = self
self.collectionView!.dataSource = self
self.collectionView.backgroundColor = UIColor.green
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionViewCell
cell.itemImage.image = UIImage(named: "banana.png")
cell.itemName.text = "banana testing"
return cell
}

我可以让 Collection View 背景更改颜色,但由于某种原因,它不会填充 Collection View 内的项目。任何提示将不胜感激。我什至调用了重新加载函数,但仍然没有区别:

DispatchQueue.main.async {
self.collectionView.reloadData()
}

最佳答案

好吧,我不久前找到了答案,我认为最好发布答案。我的代码或 Storyboard中的任何连接都没有问题。我所要做的就是进入属性检查器并在 Collection View 下,将布局从 Custom 更改为 Flow

我认为旧版本的 Xcode 会自动将布局设置为 Flow,但现在似乎默认为 Custom。我只是需要多注意一些细节。

关于swift - Collection View 不返回单元格(Swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47637674/

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