brightec / CustomCollectionViewLayout

Custom layout for a collection view using horizontal and vertical scrolling with sticky rows and columns
MIT License
519 stars 123 forks source link

Adding Header to CustomCollectionView #10

Closed Mamonaku closed 8 years ago

Mamonaku commented 8 years ago

Hello, and thanks for this great code!

I'm trying to add a header to the view, using the following:

collectionView .registerNib(UINib(nibName: "HeaderMulticolumn", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")

then later, implement the delegate methods:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) 

            headerView.backgroundColor = UIColor.blueColor();
            return headerView
}

and in the layout class


    override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withIndexPath: indexPath)

        let headerHeight = CGFloat(50)
        attributes.frame = CGRectMake(0, 0, self.collectionView!.frame.size.width, headerHeight)

        return attributes;
    }

It's not necessarily a question about your code, but I can't get collectionView: viewForSupplementaryElementOfKind: to be called at all. Is there any chance you could point at the right direction to have Headers added to your custom UICollectionView class? note: I tried with different layout classes, without changing the controller class, and managed to have headers added, so it must come from the way CustomCollectionViewLayout operates (but I can't see why) thanks!

Mamonaku commented 8 years ago

ok, found it.

ismaiI1 commented 5 years ago

@Mamonaku How did you add header row? can you tell please :)