For the More Curious: UICollectionView

The class UICollectionView is very similar to UITableView:

  • It is a subclass of UIScrollView.

  • It displays cells, although these cells inherit from UICollectionViewCell instead of UITableViewCell.

  • It has a data source that supplies it with those cells.

  • It has a delegate that gets informed about things like a cell being selected.

  • Similar to UITableViewController, UICollectionViewController is a view controller class that creates a UICollectionView as its view and becomes its delegate and data source.

How is the collection view different? A table view only displays one column of cells; this is a huge limitation on a large-screen device like an iPad. A collection view can layout those cells any way you want. The most common layout is a grid (Figure 19.15).

Figure 19.15  Homepwner with a UICollectionView

Homepwner with a UICollectionView

How does the UICollectionView figure out how to arrange the cells? It has a layout object that controls the attributes of each cell, including its position and size. These layout objects inherit from the abstract class UICollectionViewLayout. If you are just laying your cells out in a grid, you can use an instance of UICollectionViewFlowLayout. If you are doing something fancy, you will need to create a custom subclass of UICollectionViewLayout.

Figure 19.16  Example object model for a UICollectionView

Example object model for a UICollectionView

The default UITableViewCell is quite usable. (You used it for several chapters before this one.) UICollectionViewCell is not. It has a content view, but the content view has no subviews. So, if you are creating a UICollectionView, you will need to create a subclass of UICollectionViewCell.

That is all you need to know to get your first collection view up and running. After that, you will want to play around with the background view, supplementary views (which are mostly used as headers and footers for sections), and decoration views. The cell also has its own background view and a selected background view (which is laid over the background view when the cell is selected).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset