mosaic-layout icon indicating copy to clipboard operation
mosaic-layout copied to clipboard

Headers not working?

Open jalakoo opened this issue 8 years ago • 18 comments

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

jalakoo avatar Dec 06 '16 22:12 jalakoo

What kind of trouble?

vinnyoodles avatar Dec 06 '16 22:12 vinnyoodles

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()
    }

jalakoo avatar Dec 07 '16 00:12 jalakoo

Is there a stack trace for this error?

vinnyoodles avatar Dec 07 '16 00:12 vinnyoodles

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?

jalakoo avatar Dec 07 '16 19:12 jalakoo

What does your code look like for the header cell?

Nathan961207 avatar Dec 10 '16 10:12 Nathan961207

// 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!
}

jalakoo avatar Dec 10 '16 15:12 jalakoo

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)

Nathan961207 avatar Dec 10 '16 19:12 Nathan961207

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.

jalakoo avatar Dec 13 '16 17:12 jalakoo

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

Nathan961207 avatar Dec 21 '16 16:12 Nathan961207

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.

asteriomanet avatar Feb 22 '17 10:02 asteriomanet

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?

Nathan961207 avatar Feb 22 '17 11:02 Nathan961207

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

asteriomanet avatar Feb 22 '17 21:02 asteriomanet

Check this answer

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

On Wed, 22 Feb 2017 at 22:50, Jun [email protected] wrote:

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"

[image: screen shot 2017-02-23 at 5 47 05 am] https://cloud.githubusercontent.com/assets/5847140/23234037/c27cefae-f98b-11e6-8f37-8f824b249b7e.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/vinnyoodles/TRMosaicLayout/issues/9#issuecomment-281815741, or mute the thread https://github.com/notifications/unsubscribe-auth/AOmtnqz06ogF_cMJpxkG33P_MqgaXMi_ks5rfK2ygaJpZM4LF-vV .

Nathan961207 avatar Feb 22 '17 21:02 Nathan961207

Thank you.

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

asteriomanet avatar Feb 22 '17 23:02 asteriomanet

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

wjaram avatar Apr 26 '17 05:04 wjaram

I am having the same problem.

rlee0524 avatar May 31 '17 07:05 rlee0524

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

petard avatar Oct 19 '17 19:10 petard

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

sukidhar avatar Jul 11 '20 10:07 sukidhar