UICollectionViewCell from xib file not showing up in UICollectionView

雨燕双飞 提交于 2019-12-08 13:35:19

问题


I have a custom xib file which contains a label and a UICollectionView. I have a second xib file for the collection view's cell with a custom subclass of UICollectionViewCell.

The parent xib file's owner looks like below-

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()
        }
    }
}

The UICollectionViewCell xib file is named SizesCell.xib and the class file is SizesCell.swift, the code in SizesCell class looks like this-

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")
    }

}

Where PackageSize is a Struct as

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

Now the problem I am facing is that the cells simply won't load into the collection view in the parent xib file and I simply can't figure out why the init in the UICollectionViewCell class is not being called at all. I have tried it with awakeFromNib() as well, but that didn't work either. The file owners, custom classes etc. are all set correctly in the IB. What am I missing here?

来源:https://stackoverflow.com/questions/45021199/uicollectionviewcell-from-xib-file-not-showing-up-in-uicollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!