简体   繁体   中英

UICollectionViewCell doesn't appear

How can I make the UICollectionViewCell appear? The CollectionView background does appear, but the cell doesn't, but I don't understand why not. Please check this code. 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)
}

Only Check two things :

1>> If you didn't applied customCollectionViewCell then Apply.

2>> Also Take care about reuseIdentifier of that Priority Cell.

Don't Use default. Still any query ask me here.

The delegate/datasource methods for collection view is not been called in "itemsCollectionView". I just changed the itemsCollectionView call in ItemGroupCell for time being and its working

Add a view controller variable in ItemGroupCell

let itemVC : UIViewController = {

     let itemCollectionView = itemsCollectionView()
     return itemCollectionView
}()

Change the " cellForItemAtIndexPath " in MainViewController like this

 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
}

Hope its working...

Add this line in your collection view cell class as

  contentView.addSubview(imageview)

Or you can just add this line in your cell for item at index method

  yourCollectionViewCell.addSubview(yourCollectionViewCell.imageview)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM