UICollectionViewCell不会出现

时间:2016-06-28 01:08:47

标签: ios swift uicollectionview uicollectionviewcell

如何显示UICollectionViewCellCollectionView背景确实出现,但单元格没有,但我不明白为什么不。请检查此代码。 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)
}

2 个答案:

答案 0 :(得分:0)

只检查两件事:

1>>如果您没有应用customCollectionViewCell,则应用。

2 - ;>还要注意该优先级单元的reuseIdentifier。

不要使用默认值。还有任何疑问都在这里问我。

答案 1 :(得分:0)

未在“itemsCollectionView”中调用集合视图的委托/数据源方法。我刚刚更改了ItemGroupCell中的itemsCollectionView调用及其工作

ItemGroupCell

中添加视图控制器变量
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
}

希望它的工作......

相关问题