-1

UICollectionView lets you define custom views for cell items, for supplementary views, and for decorative views. These must all subclass UICollectionReusableView, so that the collection view can dispose of them to save resources when they are offscreen.

However, I want to add a view which scrolls along with the rest of the collection view's content, but which will never be disposed of. That is because this view contains some complex state I do not wish to manage in the view controller (long story). This view is a bit like a decoration view, in that it does not depend on data vended by the UICollectionViewDatasource.

So what is a valid way to do this?

Option 1. Is there a way to make this a decoration view, but somehow mark it so that the collection view will never dispose of it when it's offscreen?

Option 2. If not, is the best alternative to just add this as an ordinary subview to the collection view, taking advantage of the collection view's functionality as a scroll view? Is this supported by the collection view explicitly, or am I at risk that manually adding a subview will break the collection view's own layout management?

Option 3. If this isn't supported by collection views, then is there another conventional best practice for this case? For instance, I could add the decoration-like view as a sibling to the collection view, and then try to hook into the collection view's pan gesture recognizer, but this feels hacky and fragile.

Since

algal
  • 27,584
  • 13
  • 78
  • 80
  • Try looking into `collectionViewHeaderView` or the footer view. – duci9y Jul 05 '14 at 17:14
  • Those are supplementary views and so they are liable to be destroyed at the discretion of the collection view. This is what I'm trying to avoid. – algal Jul 05 '14 at 19:25
  • You could go about following this http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using#notes or avoid the trouble and just add a subview to the collection view's superview. – duci9y Jul 05 '14 at 19:34
  • Yeah, seems like I need to add the subview to the collection view (so I control its initialization and lifetime) but then also need to implement a custom layout (so the collection view lays out its content offset, so it won't crash into my manually added subview). – algal Jul 05 '14 at 20:08

2 Answers2

0

this view contains some complex state I do not wish to manage in the view controller

This is the root of the problem. It's never a good idea to use views to store state. You say that you're looking for a "best practice," but the truth of the matter is that the best practice is to move your state management code out of the view and into a model object.

The separation between model and view is is there pretty much for the reason you've discovered -- views have different lifetimes, relationships, and responsibilities than model classes. Any solution to your problem that tries to turn a view into a model class is likely to give you that fragile, hacky feeling.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I know this. That's why I say that part is a "long story". Nonetheless, I'd still like to know if there's a way to add a subview that tracks the contents of a UICollectionView without being managed as a reusable cell. This is what the built-in UITableView provides via tableHeaderView and tableFooterView, so perhaps there are valid use cases for this. – algal Jul 06 '14 at 05:51
0

It seems like my question is a duplicate (at least in the likely special case of a header or footer view) of How to add HeaderView in UICollectionView like UITableView's tableHeaderView .

And that answers shows that one solution is to use the contentInset property on the UICollectionView to create space for headers or footers, and then manually add them as subviews into that space.

Another solution that worked for me is to subclass UICollectionViewFlowLayout to create an additional space, instead of using contentInset.

However, using the contentInset is obviously more straightforward. The only difference I can see is that the inset-based method means that the collection view's contentSize only reflects the cells it manages, and not the manually added header and footer views. I don't see any problems with this.

Community
  • 1
  • 1
algal
  • 27,584
  • 13
  • 78
  • 80