来自xib文件的UICollectionViewCell不会在UICollectionView中显示

我有一个定制的xib文件,其中包含一个标签和一个UICollectionView 。 我有一个集合视图的单元格与UICollectionViewCell的自定义子类的第二个xib文件。

父xib文件的所有者如下所示 –

 import UIKit class PackageSizePickerVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { @IBOutlet weak var collection: UICollectionView! let sizes: [PackageSize] = { let small = PackageSize(title: "Small", imageName: "S") let medium = PackageSize(title: "Medium", imageName: "M") let large = PackageSize(title: "Large", imageName: "L") let extralarge = PackageSize(title: "Extra Large", imageName: "XL") return [small, medium, large, extralarge] }() override func viewDidLoad() { super.viewDidLoad() collection.delegate = self collection.dataSource = self collection.register(UINib(nibName: "SizesCell", bundle: nil), forCellWithReuseIdentifier: "Sizecell") //register with nib } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return sizes.count } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.frame.width, height: collectionView.frame.height) } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if let cell = collection.dequeueReusableCell(withReuseIdentifier: "Sizecell", for: indexPath) as? SizesCell { let size = sizes[indexPath.row] cell.image = size.imageName cell.title = size.title return cell }else{ return UICollectionViewCell() } } } 

在这里输入图像说明

UICollectionViewCell xib文件被命名为SizesCell.xib ,类文件是SizesCell.swift ,SizesCell类中的代码看起来像这样 –

 import UIKit class SizesCell: UICollectionViewCell { @IBOutlet weak var sizeImage: UIImageView! @IBOutlet weak var sizeLabel: UILabel! var image: String! var title: String! override init(frame: CGRect) { super.init(frame: frame) sizeImage.image = UIImage(named: image) sizeLabel.text = title } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 

在这里输入图像说明 在这里输入图像说明

在这里输入图像说明

PackageSize是一个Struct as

 struct PackageSize { let title: String let imageName: String } 

现在我面临的问题是单元格不会加载到父xib文件中的集合视图,我根本无法弄清楚为什么UICollectionViewCell类中的init根本没有被调用。 我也尝试了用awakeFromNib()以及,但也没有工作。 文件所有者,自定义类等都在IB中正确设置。 我在这里错过了什么?