mercredi 6 mai 2015

Why are cells no longer visibile in my UICollectionView?

The UICollectionViewController class

In my app there are two classes. The first one is aUICollectionViewController. Inside this class I declare a UICollectionView creating an IBOutlet connection.

The UICollectionViewLayout class

The second one, a UICollectionViewLayout, contains delegate methods such as collectionViewContentSize, layoutAttributesForElementsInRect: and layoutAttributesForItemAtIndexPath:.

class myLayout: UICollectionViewLayout {

    override func prepareLayout() {
       super.prepareLayout()
    }

    override func collectionViewContentSize() -> CGSize {
       let screenSize: CGRect = UIScreen.mainScreen().bounds
       var center = CGPoint(x: screenSize.width/2.0, y: screenSize.height/2.0)
       return super.collectionViewContentSize()
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
       if let attributes = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes] {
            for attribute in attributes {
                let center = attribute.center
            }
        return attributes
        }
       return nil
     }

    override func layoutAttributesForItemAtIndexPath(indexPath:
    NSIndexPath) -> UICollectionViewLayoutAttributes {
        let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
        return attributes
     }
}

Assigning the layout to the UICollectionView

To link the UICollectionViewLayout to the main UICollectionView, I use the collectionViewLayout property in the viewDidLoad method:

override func viewDidLoad() {
    super.viewDidLoad()

    let layout = myLayout()
    myCollectionView.collectionViewLayout = layout
}

After assigning the layout, cells are no more visibile

After running the app, I can't no more see the UICollectionViewCells of the UICollectionView. They were visibile before assigning a new layout to the UICollectionView. I can only see the background of the UICollectionView.

Why are cells no longer visible?

Aucun commentaire:

Enregistrer un commentaire