ARCollectionViewMasonryLayout icon indicating copy to clipboard operation
ARCollectionViewMasonryLayout copied to clipboard

Layout is asking for ARCollectionElementKindSectionStickyHeader??

Open glm4 opened this issue 8 years ago • 9 comments

Why the layout is asking for a reusable view for kind ARCollectionElementKindSectionStickyHeader if I never registered it to my collection or implemented the - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForStickyHeaderInSection:(NSInteger)section; method?

glm4 avatar Sep 05 '17 19:09 glm4

Hmm, it shouldn't be doing that. Can you provide some of the setup code, or a sample project that demonstrates the problem? More details about what's going on would definitely be helpful 👍

ashfurrow avatar Sep 05 '17 19:09 ashfurrow

Using 2.2.0 version of the library.

here is my configuration:

Setup:

if let layout = ARCollectionViewMasonryLayout(direction: .vertical) {
       let margin = collection.frame.width * 0.04
      layout.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
      layout.itemMargins = CGSize(width: margin, height: margin)
      layout.dimensionLength = collection.frame.width / 2 - (margin * 2)
      collection.collectionViewLayout = layout
    }
  
  let headerNib = UINib(nibName: HeaderReusableView.reuseIdentifier, bundle: nil)
    collection.register(headerNib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HeaderReusableView.reuseIdentifier)

Then delegate methods:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
      guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HeaderReusableView.reuseIdentifier, for: indexPath) as? HeaderReusableView else {
        return HeaderReusableView()
      }
      return header
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: collectionView.frame.height * 0.15)
  }

If I leave it like that it crashed and it said that the view obtained for ARCollectionElementKindSectionStickyHeader was not obtained calling dequeue... .

I had to add:

collection.register(headerNib, forSupplementaryViewOfKind: ARCollectionElementKindSectionStickyHeader, withReuseIdentifier: HeaderReusableView.reuseIdentifier) to make it work but I think this shouldn't be needed right?

glm4 avatar Sep 05 '17 20:09 glm4

Right, so when you return HeaderReusableView(), what needs to happen instead is to call this function which will create one for you. Try it out and let me know how it goes.

ashfurrow avatar Sep 05 '17 20:09 ashfurrow

But that function needs a reuse identifier previously registered, which is what I don't want to do(Or should I even its not in the documentation).

glm4 avatar Sep 05 '17 20:09 glm4

Sorry, I guess I'm confused. Can you describe the initial problem again?

ashfurrow avatar Sep 05 '17 20:09 ashfurrow

The collectionView is calling the func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView delegate method. Which I think it is not good, since I never registered that kind or implemented the referenceSizeForStickyHeaderInSection method of the layout.

glm4 avatar Sep 06 '17 14:09 glm4

Okay, thanks. I'm not sure what's going on. Have you implemented the standard, non-sticky header delegate function? There are two (source code).

To help me track down the problem and provide a solution, could you please create a sample project demonstrating the problem? Please provide as much detail as possible (what you expect, what happens instead, that kind of stuff).

ashfurrow avatar Sep 06 '17 14:09 ashfurrow

Yes, I implemented the standard(collectionView:self.collectionView layout:self referenceSizeForHeaderInSection:indexPath.section) header(the one I need, I dont want the sticky ). I'll get back to you as soon as I can create the demo project. Thanks a lot.

glm4 avatar Sep 06 '17 14:09 glm4

Okay, thanks!

ashfurrow avatar Sep 06 '17 14:09 ashfurrow