集合视图不返回单元格 (Swift 3)
Collection View not returning cells (Swift 3)
好的,所以我在 UIView 中添加了一个 UICollectionView 并添加了一个项目,但我似乎无法弄清楚为什么该项目没有显示在集合视图中。这是我目前所拥有的:
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
}
我可以让集合视图背景改变颜色,但出于某种原因,它不会填充集合视图内的项目。任何提示将不胜感激。我什至调用了重新加载函数,但仍然没有区别:
DispatchQueue.main.async {
self.collectionView.reloadData()
}
好的,所以我刚才找到了答案,我认为最好发布答案。我的代码或情节提要中的任何连接都没有问题。我所要做的就是进入属性检查器并在 Collection View 下,将布局从 Custom
更改为 Flow
。
我认为 Xcode 的旧版本会自动将布局设置为 Flow
,但现在似乎默认为 Custom
。我只是需要多注意一些细节。
好的,所以我在 UIView 中添加了一个 UICollectionView 并添加了一个项目,但我似乎无法弄清楚为什么该项目没有显示在集合视图中。这是我目前所拥有的:
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
}
我可以让集合视图背景改变颜色,但出于某种原因,它不会填充集合视图内的项目。任何提示将不胜感激。我什至调用了重新加载函数,但仍然没有区别:
DispatchQueue.main.async {
self.collectionView.reloadData()
}
好的,所以我刚才找到了答案,我认为最好发布答案。我的代码或情节提要中的任何连接都没有问题。我所要做的就是进入属性检查器并在 Collection View 下,将布局从 Custom
更改为 Flow
。
我认为 Xcode 的旧版本会自动将布局设置为 Flow
,但现在似乎默认为 Custom
。我只是需要多注意一些细节。