Adding a Decoration View to a UICollectionView in Interface Builder

tl;dr: You Can’t. But…

My current project employs multiple UICollectionViews with custom layouts. Those layouts have a nice decoration view that I borrowed from Joe Keely and Kyle Richter (repo here), which puts a sleek, modern IKEA style ‘shelf’ behind the collectionView’s

Saweeet UIDecorationView
Saweeet UIDecorationView

cells (i.e., the ‘books’).

However, I kept getting an error, which the debugger translated as “You need to register your decoration view as a class or a nib, or prototype it in storyboard.” This, I was happy to do, except that storyboard does not provide decorationView protoyping, just header/footer views, and of course, the collectionViewCells themselves.

…you can register them in initWithCoder.

Imagine my surprise when despite all my efforts to register the durn view, Xcode was not in the least impressed. To make a long story short, I remembered that when initializing an object in Storyboard, initWithCoder is called (rather than init). I do not recall exactly which Apple docs gave me this nugget (please let me know in the comments), but I believe it was some chain of hyperlinks bouncing off NSCopying and NSCoder protocols.

In hindsight, there it is, clear as day, on SO. But duplication of answers to debug questions is (often) Good for the Internet, providing a larger bullseye for a questioner to hit. Happy Bug Hunting y’all!

…and we’re back!

UICollectionViews and I (part I)

tl;dr – one-line designated-init-boilerplate

If you take nothing else from this post, here is a clever LOC I encountered in Ash Furrow’s book on UICollectionViews:

Ok, on to the post (or skip to the summary):

First Thing: Apologies.

My apologies to you, my adoring audience and fans, during the unexpected and unexplained interruption. I cannot change your expectations, but I can provide an explanation: I switched web hosts to save $100, and learned much about the process in doing so. My biggest lesson (read “mistake from which I am determined to squeeze out something constructive”) was in regards to the WordPress Migration plugin.

Namely, I learned that It Exists! Never migrate by hand again; next time you are lost in the woods with your celebrity crush, and desperately need to find food, shelter, and to migrate your wordpress site, use this swiss army knife to look suave in front of your million dollar companion as you save the day!

Second Thing: Some Ash Furrow tips on UICollectionView

I have been working on an app for my portfolio that uses UICollectionViews, and ideally, needs a custom layout. This is a fun challenge, since it involves a bit more of my math background, but I really appreciate the tidbits an experienced dev throws in when showing you around a favorite piece of tech. Thus, I am going through Ash Furrow’s book on UICollectionViews, and thought to share some of the tidbits I picked up so far, halfway through.

Furrowisms

  • XIBs and Storyboards are not really worth it for UICollectionViews, (with the possible exception of just having a blank one for the initialization code). Why?

“There are two problems with storyboards and collection views: the tight coupling between the collection view cell (its reuse identifier) and the code, and some tricky debugging when you modify the settings of the storyboard in code at runtime. You can’t rely on what you see visually at compile time because it’s likely going to be changed by code at runtime, anyway.” (Kindle Locations 834-836)

  • “[Use] custom UICollectionViewCell subclasses to keep your code loosely coupled; your view controller shouldn’t know about the internals of the cell’s view hierarchy.” (Kindle Locations 840-841)

Summary Conclusion

  • Clever 1-line init boilerplate.
  • WordPress Migration plugin for painless blog migration.
  • I’m knee-deep in UICollectionViews, and aim to present a recipe/tutorial on layouts soon, knock on wood.
  • Homage to Ash Furrow _|-\o_