如何使 UICollectionViewCell
出现? CollectionView
背景确实出现,但单元格没有出现,但我不明白为什么不出现。请检查此代码。 Full source code is here (github)
class itemsCollectionView: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var myItemCollectionView: UICollectionView!
override func viewDidLoad() {
view.backgroundColor = UIColor.redColor()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
layout.itemSize = CGSizeMake(50, 50)
layout.minimumInteritemSpacing = CGFloat(5.0)
layout.scrollDirection = .Horizontal
myItemCollectionView = UICollectionView(frame: CGRectMake(5, 5, 300, 100), collectionViewLayout: layout)
myItemCollectionView.dataSource = self
myItemCollectionView.delegate = self
myItemCollectionView.registerClass(ItemCell.self, forCellWithReuseIdentifier: CELLID)
myItemCollectionView.backgroundColor = UIColor.darkGrayColor()
self.view.addSubview(myItemCollectionView)
}
最佳答案
Collection View 的委托(delegate)/数据源方法未在“itemsCollectionView”中调用。我只是暂时更改了 ItemGroupCell 中的 itemsCollectionView 调用及其工作
在ItemGroupCell中添加 View Controller 变量
let itemVC : UIViewController = {
let itemCollectionView = itemsCollectionView()
return itemCollectionView
}()
像这样更改MainViewController中的“cellForItemAtIndexPath”
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let groupCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifierID, forIndexPath: indexPath) as! ItemGroupCell
// if let groupName = groupMenu[indexPath.item].groupName {
// groupCell.groupName.text = groupName
// }
// Configure the cell
groupCell.Info = groupInfo[indexPath.item]
groupCell.addSubview(groupCell.itemVC.view)
groupCell.addConstraintsWithFormat("H:|-15-[v0]-15-|", views: groupCell.itemVC.view)
return groupCell
}
希望它能正常工作...
关于ios - UICollectionViewCell 不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38065533/