iOS UICollectionView: The Complete Guide, Second Edition (2014)
At WWDC 2012, Apple unveiled UICollectionView, enabling a new way for apps to render content to users. Collection views are a content- and layout-agnostic tool for developers to display content in apps. User interfaces created with collection views are some of the most immersive, distinctive interfaces in iOS applications.
However, the power afforded to developers by collection views is balanced by the complexity of using them. As the saying goes, Cocoa makes common things easy and uncommon things possible. UICollectionView embodies this sentiment.
I said earlier that collection views are layout-agnostic, and that’s true: Developers write their own layouts for collection views to use to organize their content on the screen. Luckily, Apple included a sample layout that displays grids, a common request among developers.
How to Use This Book
This book is meant to tell a story; each chapter builds upon the last one to guide readers through every nook and cranny of UICollectionView. I strongly encourage readers to read each chapter in sequence and follow along with the code samples.
The first chapter makes sure that readers have a common vocabulary when discussing the organization of code in iOS applications. Even if you’re a seasoned developer, it’s worth a look just to make sure you’re on the same page as I am.
The code provided with this book is as valuable as the explanations in the chapters of why the code is written the way it is.
All of the code that appears in this book can be downloaded at http://ashfurrow.com/uicollectionview-the-complete-guide/.
Who This Book Is For
This book is for intermediate to advanced iOS developers who want to take full advantage of UICollectionView. If you’re trying to write your first-ever iOS application, this book probably isn’t for you. I’ve written this book with the assumption that you understand the concepts of objects and view hierarchies, as well as basic Objective-C syntax.
Organization of This Book
This book is organized into six chapters to guide readers through a comprehensive description of every aspect of collection views:
Chapter 1, “Understanding Model-View-Controller on iOS,” briefly introduces the MVC paradigm of application architecture that’s used throughout the remainder of the book.
Chapter 2, “Displaying Content Using UICollectionView,” introduces readers to UICollectionView with some basic examples using .xib files and storyboards, as well as view setup using only code. This chapter ends with a case study on application performance tuning.
Chapter 3, “Contextualizing Content,” builds on the basics of cell use from Chapter 2 to explain how to contextualize content for users by using supplementary views. The chapter explores the UICollectionViewDataSource and UICollectionViewDelegate protocols as well.
Chapter 4, “Organizing Content with UICollectionViewFlowLayout,” introduces readers to the idea of creating their own custom layouts while relying on existing logic in UICollectionViewFlowLayout. The sample code from Chapter 3 is augmented with decoration views, and custom collection view attributes are used to customize cell layout. The chapter ends with a look at a Cover Flow-esque layout.
Chapter 5, “Crafting Custom Layouts Using UICollectionViewLayout,” explains to readers who understand subclassing flow layouts that they can subclass UICollectionViewLayout directly for incredibly custom layouts. The chapter also covers changing layouts with animation support, as well as provides some further examples on how to use supplementary views and decoration views with completely custom layouts.
Chapter 6, “Adding Interactivity to UICollectionView,” is the crown jewel of this book. It looks back at all the previous chapters’ code samples to augment them with interactivity, mostly using gesture recognizers. Additionally, it shows off how to use UIKit Dynamics, a new animation library in iOS 7.
What’s New in the Second Edition
The second edition of this book covers what’s new in UICollectionViews in iOS 7. It removes some gotchas that were present in iOS 6 but were fixed in iOS 7, and it details a few new ones. This book also covers how to use UICollectionViews with UIKit Dynamics, an exciting new iOS 7 technology.