子类UICollectionViewCell不显示

时间:2018-08-04 13:52:07

标签: ios swift uicollectionview uicollectionviewcell

我是Swift的新手,我打算以编程方式创建带有UICollectionViewCell子类实例的UICollectionView。但是,单元格本身不显示。

以下大多数代码均来自本教程:iOS Swift: Build UICollectionView programmatically without Storyboards

这是我创建UICollectionViewController的方法:

let flowLayout = UICollectionViewFlowLayout()
    let productView = ProductView(collectionViewLayout: flowLayout)
    productView.setSizeOfCollectionView(width, height: (height * 0.7))

    middleLeftView.addSubview(productView.view)

这些是我的子类:

    class ProductView : UICollectionViewController, UICollectionViewDelegateFlowLayout {

    let customCellIdentifier = "customCellIdentifier"

    var products = [[String]]()

    var product1 = ["beer", "2,50"]
    var product2 = ["wine", "3,50"]


    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = UIColor.redColor()

        collectionView?.delegate = self
        collectionView?.dataSource = self

        products.append(product1)
        products.append(product2)

        collectionView?.registerClass(Product.self, forCellWithReuseIdentifier: customCellIdentifier)

        self.collectionView?.reloadData()
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let product = collectionView.dequeueReusableCellWithReuseIdentifier(customCellIdentifier, forIndexPath: indexPath) as! Product
        product.specifiyProduct(products[indexPath.item])
        product.backgroundColor = UIColor.blueColor()
        return product
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(products.count) // prints 2
        return products.count
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(300, 300)
    }

    func setSizeOfCollectionView(width: CGFloat, height: CGFloat){
        print(width) // prints 1022.5
        print(height) // prints 702.8
        collectionView?.frame = CGRectMake(10, 10, (width - 20), (height - 20))
        self.collectionView?.reloadData()
    }
}
class Product : UICollectionViewCell {

    override init(frame: CGRect){
        super.init(frame: frame)
        self.backgroundColor = UIColor.yellowColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func specifiyProduct(info: [String]){
        let nameLabel = UILabel(frame: CGRectMake(0, 0, 50, 50))
        nameLabel.backgroundColor = UIColor.grayColor()
        nameLabel.text = info[0] + info[1]
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        addSubview(nameLabel)

        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|" , options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[v0]|" , options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
    }

}

出现UICollectionView,但没有显示UICollectionViewCells。有谁知道如何解决这一问题?

1 个答案:

答案 0 :(得分:0)

尝试更改 collectionView?.registerClass(Product.self, forCellWithReuseIdentifier: customCellIdentifier)

collectionView?.register(Product.self, forCellWithReuseIdentifier: customCellIdentifier)

用于注册单元格的方法已重命名为后一种方法。这可能是原因。