9

I've been hunting around for ways to setup a UICollectionView for an iOS app. Google only turns up a few blogs with posts about what it is, not how it works. Then of course, there's the Apple documentation which is helpful, but doesn't provide as much information as I'd like to be able to setup a UICollectionView.

How can one setup a UICollectionView?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
  • 1
    I strongly recommend you view the WWDC 2012 videos at developer.apple.com. These videos show what can be done with a UICollectionView and also take you through how to setup and write the code. – Robotic Cat Sep 29 '12 at 16:21
  • @RoboticCat What are the titles of the relevant videos? I know #209 and #215 appear to be interesting, but actually finding these on the very bad website is a chore. – SpacyRicochet Oct 04 '12 at 07:46
  • @SpacyRicochet: The two main videos are #205 - Introducing Collection Views and then #219 - Advanced Collection Views and Building Custom Layouts (I think you meant these?). UICollectionViews are mentioned in another couple of videos but I forget which. Also note that the API names changed (slightly) between the videos; check the iOS 6 release notes and the iOS6 API diffs as per normal (enhanced animation customisation not in the videos and method renames). – Robotic Cat Oct 04 '12 at 15:59

2 Answers2

32

The class is almost identical to the Class. They share many of the same methods and functions. And if the methods / functions are different, most of the time it's just a matter of swapping out "row" for "cell" and vice versa. However there are a few methods that don't exist on UICollectionView that do on UITableView. First though, I'll explain how to setup a UICollectionView:

  1. Begin by adding your UICollectionView to a current ViewController, or creating a new UICollectionViewController. The steps aren't that much different for the view and controller.
  2. If you're using the View and not the ViewController, make sure that the Delegate and DataSource of the CollectionView is the view controller it's on. Also make sure to add the Delegate and DataSource to your header file: <UICollectionViewDataSource, UICollectionViewDelegate>

  3. Next, make sure to include these three methods in your view controller's class:

    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    
  4. These are the only required methods. The first tells the collection view the number of sections it should have. This should return an integer value. The second method gets the number of cells in each section. Again, this should return an integer value. The last method populates each cell using the data given (usually from an NSArray). This last method should return a CollectionViewCell. If you set breakpoints on this method, you'll notice that it is called once for every cell defined in the numberOfItemsInSection method.

UICollectionViews provide advanced animation methods and allow cells to be deselected and selected (similar to apps like Pages when in 'Edit' mode). However, to my knowledge, UICollectionViews do not provide features such as "swipe to delete" or and kind of disclosure indicator.

UICollectionViews also allow you to create custom cells using (AKA ) files, this allows for some very advanced-looking and unique interfaces without lots of complicated code.

Sadly, UICollectionView is only supported in iOS 6 and up. There are a few projects available such as PSTCollectionView which adds support for CollectionViews in iOS 4.3+, but I haven't figured out how to use them. In my case, when the view loads I just check if the UICollectionView Class is available and if it isn't then I load a Table instead.

Here is a link to Apple's official documentation on Collection Views. You might also want to check out this tutorial.

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
25

I created a step-by-step tutorial for setting up UICollectionViews with custom layouts. Hopefully it helps some people to get familiar with the API.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
bryguy1300
  • 901
  • 9
  • 11