When adding a collection view to your user interface, your app’s main job is to manage the data associated with that collection view. The collection view gets its data from the data source object, which is an object that conforms to the UICollectionViewDataSource
protocol and is provided by your app. Data in the collection view is organized into individual items, which can then be grouped into sections for presentation. An item is the smallest unit of data you want to present. For example, in a photos app, an item might be a single image. The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell
class that your data source configures and provides.
In addition to its cells, a collection view can present data using other types of views too. These supplementary views can be things like section headers and footers that are separate from the individual cells but still convey some sort of information. Support for supplementary views is optional and defined by the collection view’s layout object, which is also responsible for defining the placement of those views.
Besides embedding it in your user interface, you use the methods of UICollectionView
object to ensure that the visual presentation of items matches the order in your data source object. Thus, whenever you add, delete, or rearrange data in your collection, you use the methods of this class to insert, delete, and rearrange the corresponding cells. You also use the collection view object to manage the selected items, although for this behavior the collection view works with its associated delegate
object.
Collection Views and Layout Objects
A very important object associated with a collection view is the layout object, which is a subclass of the UICollectionViewLayout
class. The layout object is responsible for defining the organization and location of all cells and supplementary views inside the collection view. Although it defines their locations, the layout object does not actually apply that information to the corresponding views. Because the creation of cells and supplementary views involves coordination between the collection view and your data source object, the collection view actually applies layout information to the views. Thus, in a sense, the layout object is like another data source, only providing visual information instead of item data.
You normally specify a layout object when creating a collection view but you can also change the layout of a collection view dynamically. The layout object is stored in the collectionViewLayout
property. Setting this property directly updates the layout immediately, without animating the changes. If you want to animate the changes, you must call the setCollectionViewLayout:animated:completion:
method instead.
If you want to create an interactive transition—one that is driven by a gesture recognizer or touch events—use the startInteractiveTransitionToCollectionViewLayout:completion:
method to change the layout object. That method installs an intermediate layout object whose purpose is to work with your gesture recognizer or event-handling code to track the transition progress. When your event-handling code determines that the transition is finished, it calls the finishInteractiveTransition
or cancelInteractiveTransition
method to remove the intermediate layout object and install the intended target layout object.
Creating Cells and Supplementary Views
The collection view’s data source object provides both the content for items and the views used to present that content. When the collection view first loads its content, it asks its data source to provide a view for each visible item. To simplify the creation process for your code, the collection view requires that you always dequeue views, rather than create them explicitly in your code. There are two methods for dequeueing views. The one you use depends on which type of view has been requested:
Collection (聚集) 视图 数据源对象同时提供了items的内容和展示内容的视图。当collection 视图第一次载入它的内容的时候,它会请求数据源来为每个可视项提供一个视图。为了简化你的开发过程中的代码,collection 视图要求 从队列中取出视图(内容视图),而不是在代码中明确地创建他们。从队列中取出视图的方法有两种。这取决于哪一种视图被使用了。
Before you call either of these methods, you must tell the collection view how to create the corresponding view if one does not already exist. For this, you must register either a class or a nib file with the collection view. For example, when registering cells, you use the registerClass:forCellWithReuseIdentifier:
or registerNib:forCellWithReuseIdentifier:
method. As part of the registration process, you specify the reuse identifier that identifies the purpose of the view. This is the same string you use when dequeueing the view later.
在你调用这两者之间的一种方法之前,一定要通知collection 视图 怎样来创建相应的视图如果它还没有存在的话。 对此,你一定要向collection视图注册一个类或着nib 文件(就是相关联的类或者nib文件)。比如说,注册cells的时候,使用 registerClass:forCellWithReuseIndentifier:或者 registerNib:forCellWithReuseIndentifier:方法。 作为注册过程的一部分,必须要明确 重用标记 来标记目标视图。 这和你后面取出视图所用的字符串是一样的。
After dequeueing the appropriate view in your delegate method, configure its content and return it to the collection view for use. After getting the layout information from the layout object, the collection view applies it to the view and displays it.
在你的代理方法中取出相应的视图后,配置它内容 然后将它返回给collection 视图来使用。在从layout类中获取布局信息之后,collection 视图会把它应用视图上去并显示它。
For more information about implementing the data source methods to create and configure views, see UICollectionViewDataSource Protocol Reference.
For more information about appearance and behavior configuration, see Collection Views in UIKit User Interface Catalog.