Ios 子类UICollectionViewCell未显示
我是Swift新手,我打算以编程方式创建一个带有Ios 子类UICollectionViewCell未显示,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我是Swift新手,我打算以编程方式创建一个带有UICollectionViewCell子类实例的UICollectionView。然而,细胞本身并没有出现 以下代码的大部分派生自本教程: 这就是我如何创建UICollectionViewController: let flowLayout = UICollectionViewFlowLayout() let productView = ProductView(collectionViewLayout: flowLayout) productVi
UICollectionViewCell
子类实例的UICollectionView
。然而,细胞本身并没有出现
以下代码的大部分派生自本教程:
这就是我如何创建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)
用于注册单元格的方法已重命名为后面的方法。这可能就是原因。您在哪里创建了单元,在情节提要或xib文件中?您还需要执行contentView.addSubview(nameLabel)