作为一个完整的新手,我按照下面链接的教程设计了一个非常相似设计的新 View 。 Link Here
最终,我想显示一个 Collection View ,其中一个图像显示在一个命名上面图像的标签上方。例如。下面显示了带有“牛肉”的牛的图片。一只鸡,下面是“鸡”,你明白了。
我创建了一个 UICollectionViewController
,称为 My Meals,作为主 Storyboard入口点的转场。 UICollectionViewController
,我的饭菜,嵌入到导航 Controller 中。这个 View 有它自己的 Cocoa Touch 类,它是在身份检查器中看到的“MyMealsViewController.swift”。
我创建了一个名为“CollectionViewCell.swift”的 Cocoa Touch 类。我创建了一个带有 UIImageView
的单元格和 Collection View 上的标签。 UIImageView
和Label已经连接到CollectionView.swift,如下:
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var mainIngredientImage: UIImageView!
@IBOutlet weak var mainIngredientLabel: UILabel!
}
该单元也被赋予了类
CollectionViewCell
如身份检查员所见。我采用的代码,希望视频中的一个字符一个字符在这里:
import UIKit
class MyMealsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet weak var collectionView: UICollectionView!
let ingredients = ["Beef", "Chicken", "Duck", "Lamb", "Pork", "Saugage", "Seafood", "Turkey", "Other"]
let ingredientsImages: [UIImage] = [
UIImage(named: "Beef")!,
UIImage(named: "Chicken")!,
UIImage(named: "Duck")!,
UIImage(named: "Lamb")!,
UIImage(named: "Pork")!,
UIImage(named: "Sausage")!,
UIImage(named: "Seafood")!,
UIImage(named: "Turkey")!,
UIImage(named: "Other")!,
]
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ingredients.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
cell.mainIngredientLabel.text = ingredients[indexPath.item]
cell.mainIngredientImage.image = ingredientsImages[indexPath.item]
return cell
}
}
我希望使用的图像按照代码命名,它们都在 Assets.xassets 文件夹中等待。
我面临的问题是,当我运行模拟器时,页面会加载,背景颜色会加载。然而就是这样。没有标签。没有图像。我会理解是否有标签,但似乎与单元格无关。
我已尝试更改
UIImageView
来自 Aspect Fill、Aspect Fit 和 Scale to Fill。一切都无济于事。请问有人可以推荐我误入歧途的地方吗?是的,我也明白我必须从某个地方开始,这个视频可能不是最容易理解的,但它看起来像我希望创建的!任何问题,或者我在新手状态下遗漏的任何内容,请提出。
最佳答案
您错过了他在 7:25 in the video 中提到的“两件重要的事情”。 – 也就是说,您需要设置您的收藏 View 的dataSource
和 delegate
到您的 View Controller :
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
}
关于ios - TableView Controller 没有显示 Assets 文件夹中的内容,或者根本没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61331947/