Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers not working? #9

Open
jalakoo opened this issue Dec 6, 2016 · 18 comments
Open

Headers not working? #9

jalakoo opened this issue Dec 6, 2016 · 18 comments
Labels

Comments

@jalakoo
Copy link

jalakoo commented Dec 6, 2016

Anyone else have trouble adding collectionview headers with the Mosaic Layout?

@vinnyoodles
Copy link
Owner

What kind of trouble?

@jalakoo
Copy link
Author

jalakoo commented Dec 7, 2016

Hey @vinnyoodles, thanks for the prompt reply. I'm not able to get the following delegate command to be called:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

to create the header cell. Below is how I'm setting up the mosaic layout:

    func setupCollectionView() {
        
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseHeaderIdentifier)

        let mosaicLayout = TRMosaicLayout()
        mosaicLayout.delegate = self

        self.collectionView?.collectionViewLayout = mosaicLayout
        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self

        fetchTestData()
    }

@vinnyoodles
Copy link
Owner

Is there a stack trace for this error?

@jalakoo
Copy link
Author

jalakoo commented Dec 7, 2016

Nope, no error, header cell just never appears from a modified version of the TRCollectionViewController. Thought it was my implementation (probably still is), but I built a sample app with a basic collectionViewController and was able to get working headers to appear.

So my thought now is that I'm missing an extra setting or setup element to permit headers with the mosaic tiles?

@Nathan961207
Copy link

What does your code look like for the header cell?

@jalakoo
Copy link
Author

jalakoo commented Dec 10, 2016

// Collection View Header method (never appears to be called)
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: reuseHeaderIdentifier, for: indexPath) as! LiveFeedHeaderCell
        
        headerView.backgroundColor = UIColor.red
        
        return headerView
    }

// LiveFeedHeaderCell (stub at the moment)
import UIKit

class LiveFeedHeaderCell: UICollectionReusableView {
    
    @IBOutlet weak var label: UILabel!
}

@Nathan961207
Copy link

Nathan961207 commented Dec 10, 2016

When you built the CollectionView did you use storyboard/xib or just code?

Edit:
It looks like you are trying to register your header like you would register a cell, try to do something like this,

yourCollectionViewName.register(CellName.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: reuseHeaderIdentifier)

@jalakoo
Copy link
Author

jalakoo commented Dec 13, 2016

Thanks @Nathan961207. Using Storyboard. I updated the registration call to:

self.collectionView!.register(LiveFeedHeaderCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier:reuseHeaderIdentifier)

But a header still does not show nor does the following delegate get called:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

In Storyboard I've set the header cell background color to Blue, and in code I set it to Red, but neither are taking affect.

@Nathan961207
Copy link

Nathan961207 commented Dec 21, 2016

When building prototype cells (or headers) in the Storyboard, you should not register the cell for that class on the collectionView in code. This will overwrite your existing view and they won't appear.

http://stackoverflow.com/questions/33343973/swift-uicollectionview-custom-header-in-custom-layout-not-showing/33350781#33350781

@asteriomanet
Copy link

Hi,

I also bumped to this kind of issue just recently.
Header is not showing.
I place a breakpoint at viewForSupplementaryElementOfKind delegate, however it doesn't get in to that area area of the code.

I tried commenting out this part of the code:
// let mosaicLayout = TRMosaicLayout()
// self.collectionView?.collectionViewLayout = mosaicLayout
// mosaicLayout.delegate = self

Result: The header came out and also I pass to the area where breakpoint is set.

@Nathan961207
Copy link

So when you are not using TRMosaicLayout the header works and when you use TRMosaicLayout it does not work? Could you provide more info about your code?

@asteriomanet
Copy link

asteriomanet commented Feb 22, 2017

Hi,

Thanks for the reply.

I just did some modification on the sample project.
Added a header to it.

TRCollectionViewController.swift - I added this code

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        var reusableView:UICollectionReusableView?
        if kind == UICollectionElementKindSectionHeader {
          
            let headerView:MosaicHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "MosaicHeaderView", for: indexPath) as! MosaicHeaderView
            reusableView =  headerView   

        }

        return reusableView!   
}

MosaicHeaderView.swift - New file, no content in it though

import UIKit
class MosaicHeaderView: UICollectionReusableView {
}

Main.Storyboard - Added a header view to it, with a reuse identifier of "MosaicHeaderView"

screen shot 2017-02-23 at 5 47 05 am

@Nathan961207
Copy link

Nathan961207 commented Feb 22, 2017 via email

@asteriomanet
Copy link

asteriomanet commented Feb 22, 2017

Thank you.

Yes, I didn't register my story board header cell.

@wjaram
Copy link

wjaram commented Apr 26, 2017

Has anyone solved this because I'm experiencing the same behavior. I can't get viewForSupplementaryElementOfKind to be called when I use TRMosaicLayout

@rlee0524
Copy link

I am having the same problem.

@petard
Copy link

petard commented Oct 19, 2017

I'm having the same issue, was using FlowLayout before but after switching to TRMosaicLayout the header disappeared

@sukidhar
Copy link

did anyone find the fix, I am using customcollectionViewLayout and my header related functions are not executing and I don't know how to fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants