[英]UICollectionViewController not showing subviews of custom UICollectionViewCell
I intend to make an overview of products using custom cells in a collectionview. 我打算使用collectionview中的自定义单元格来概述产品。 I have set everything up, I get no errors, but somehow the contents of the cell dont' get shown.
我已经完成了所有设置,没有出现任何错误,但是以某种方式未显示单元格的内容。
I have 我有
self.collectionView!.registerClass(ProductCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView!.registerClass(ProductCell.self, forCellWithReuseIdentifier: reuseIdentifier)
注册了自定义单元格self.collectionView!.registerClass(ProductCell.self, forCellWithReuseIdentifier: reuseIdentifier)
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var items: Int = 0
if (section == 0) {
if (currentCategory == nil) {
items = sharedStore.categories.count
} else {
items = currentCategory.categories.count
}
} else if (section == 1) {
if (currentCategory == nil) {
items = sharedStore.products.count
} else {
items = currentCategory.items.count
}
}
return items
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ProductCell
cell.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.05)
cell.layer.cornerRadius = 5
//initialize shit
cell.titleLabel = UILabel()
cell.priceLabel = UILabel()
if(indexPath.section == 0) {
let current: Category!
//get model object
if(currentCategory == nil) {
if(sharedStore.categories.count > 0) {
current = sharedStore.categories[indexPath.row] as! Category
} else {
current = nil
}
} else {
if(currentCategory.categories.count > 0 ) {
current = currentCategory.categories[indexPath.row] as! Category
} else {
current = nil
}
}
cell.itemImage = UIImageView(image: UIImage(named: "folderImage.png"))
cell.itemImage.hidden = false
cell.titleLabel.text = current.title as String
cell.priceLabel.hidden = true
}
return cell
}
The cell is empty. 单元格为空。 No image, no title, and no pricelabel.
没有图像,没有标题,也没有价格标签。 I know the datasource is working properly, because when I add a product, it shows a new cell.
我知道数据源工作正常,因为当我添加产品时,它会显示一个新的单元格。
Any suggestions on how I can get my items in the cell? 关于如何将物品放入牢房的任何建议?
Any help would be highly appreciated :) 任何帮助将不胜感激:)
I figured it out: For some reason, when you add subviews to a custom UICollectionViewcell in a storyboard, it doesn't automatically add them to the cell when you run it. 我知道了:由于某种原因,当您将子视图添加到情节提要中的自定义UICollectionViewcell时,运行时它不会自动将其添加到单元格中。 Therefore, you'll have to do
cell.addSubview(subview)
for all properties of that cell, ether in the custom cell class or in the cellForItemAtIndex. 因此,您必须对该单元格的所有属性(自定义单元格类或cellForItemAtIndex中的以太币
cell.addSubview(subview)
进行cell.addSubview(subview)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.