gpt4 book ai didi

ios - 子类 UICollectionViewCell 不显示

转载 作者:行者123 更新时间:2023-11-28 23:53:15 26 4
gpt4 key购买 nike

我是 Swift 的新手,我打算以编程方式创建一个 UICollectionView,其中包含 UICollectionViewCell 的子类实例。然而,细胞本身并没有出现。

以下大部分代码源自本教程: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 没有出现。有谁知道如何解决这一问题?

最佳答案

努力改变collectionView?.registerClass(Product.self, forCellWithReuseIdentifier: customCellIdentifier)

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

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

关于ios - 子类 UICollectionViewCell 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51686272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com