Manages an ordered collection of data items and presents them using customizable layouts.


Figure 1

A collection view using the flow layout

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 UICollection​View​Data​Source 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 UICollection​View​Cell 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 UICollection​View 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 UICollection​View​Layout 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 collection​View​Layout property. Setting this property directly updates the layout immediately, without animating the changes. If you want to animate the changes, you must call the set​Collection​View​Layout(_:​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 start​Interactive​Transition(to:​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 finish​Interactive​Transition() or cancel​Interactive​Transition() 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:

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 register(_:​for​Cell​With​Reuse​Identifier:​) or register(_:​for​Cell​With​Reuse​Identifier:​) 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.

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.

For more information about implementing the data source methods to create and configure views, see UICollection​View​Data​Source.

For more information about appearance and behavior configuration, see Collection Views.

Reordering Items Interactively

Collection views allow you to move items around based on user interactions. Normally, the order of items in a collection view is defined by your data source. If you support the ability for users to reorder items, you can configure a gesture recognizer to track the user’s interactions with a collection view item and update that item’s position.

To begin the interactive repositioning of an item, call the begin​Interactive​Movement​For​Item(at:​) method of the collection view. While your gesture recognizer is tracking touch events, call the update​Interactive​Movement​Target​Position(_:​) method to report changes in the touch location. When you are done tracking the gesture, call the end​Interactive​Movement() or cancel​Interactive​Movement() method to conclude the interactions and update the collection view.

During user interactions, the collection view invalidates its layout dynamically to reflect the current position of the item. If you do nothing, the default layout behavior repositions the items for you, but you can customize the layout animations if you want. When interactions finish, updates its data source object with the new location of the item.

The UICollection​View​Controller class provides a default gesture recognizer that you can use to rearrange items in its managed collection view. To install this gesture recognizer, set the installs​Standard​Gesture​For​Interactive​Movement property of the collection view controller to true.

Interface Builder Attributes

Table 1 lists the attributes that you configure for collection views in Interface Builder.

Table 1

Collection view attributes




The number of prototype cells. This property controls the specified number of prototype cells for you to configure in your storyboard. Collection views must always have at least one cell and may have multiple cells for displaying different types of content or for displaying the same content in different ways.


The layout object to use. Use this control to select between the UICollection​View​Flow​Layout object and a custom layout object that you define.

When the flow layout is selected, you can also configure the scrolling direction for the collection view’s content and whether the flow layout has header and footer views. Enabling header and footer views adds reusable views to your storyboard that you can configure with your header and footer content. You can also create those views programmatically.

When a custom layout is selected, you must specify the UICollection​View​Layout subclass to use.

When the Flow layout is selected, the Size inspector for the collection view contains additional attributes for configuring flow layout metrics. Use those attributes to configure the size of your cells, the size of headers and footers, the minimum spacing between cells, and any margins around each section of cells. For more information about the meaning of the flow layout metrics, see UICollection​View​Flow​Layout.


A collection view has no direct content of its own to internationalize. Instead, you internationalize the cells and reusable views of the collection view. For more information about internationalization, see Internationalization and Localization Guide.


A collection view has no content of its own to make accessible. If your cells and reusable views contain standard UIKit controls such as UILabel and UIText​Field, you can make those controls accessible. When a collection view changes its onscreen layout, it posts the UIAccessibility​Layout​Changed​Notification notification.

For general information about making your interface accessible, see Accessibility Programming Guide for iOS.


Initializing a Collection View

init(frame:​ CGRect, collection​View​Layout:​ UICollection​View​Layout)

Initializes and returns a newly allocated collection view object with the specified frame and layout.

Configuring the Collection View

var delegate:​ UICollection​View​Delegate?

The object that acts as the delegate of the collection view.

var data​Source:​ UICollection​View​Data​Source?

The object that provides the data for the collection view.

var background​View:​ UIView?

The view that provides the background appearance.

Prefetching Collection View Cells and Data

UICollection​View provides two prefetching techniques you can use to improve responsiveness:

  • Cell prefetching prepares cells in advance of the time they are required. When a collection view requires a large number of cells simultaneously—for example, a new row of cells in grid layout—the cells are requested earlier than the time required for display. Cell rendering is therefore spread across multiple layout passes, resulting in a smoother scrolling experience. Cell prefetching is enabled by default.

  • Data prefetching provides a mechanism whereby you are notified of the data requirements of a collection view in advance of the requests for cells. This is useful if the content of your cells relies on an expensive data loading process, such as a network request. Assign an object that conforms to the UICollection​View​Data​Source​Prefetching protocol to the prefetch​Data​Source property to receive notifications of when to prefetch data for cells.

var is​Prefetching​Enabled:​ Bool

Denotes whether cell and data prefetching are enabled.

var prefetch​Data​Source:​ UICollection​View​Data​Source​Prefetching?

The object that acts as the prefetching data source for the collection view, receiving notifications of upcoming cell data requirements.

Creating Collection View Cells

func register(Any​Class?, for​Cell​With​Reuse​Identifier:​ String)

Register a class for use in creating new collection view cells.

func register(UINib?, for​Cell​With​Reuse​Identifier:​ String)

Register a nib file for use in creating new collection view cells.

func register(Any​Class?, for​Supplementary​View​Of​Kind:​ String, with​Reuse​Identifier:​ String)

Registers a class for use in creating supplementary views for the collection view.

func register(UINib?, for​Supplementary​View​Of​Kind:​ String, with​Reuse​Identifier:​ String)

Registers a nib file for use in creating supplementary views for the collection view.

Changing the Layout

var collection​View​Layout:​ UICollection​View​Layout

The layout used to organize the collected view’s items.

func set​Collection​View​Layout(UICollection​View​Layout, animated:​ Bool)

Changes the collection view’s layout and optionally animates the change.

func set​Collection​View​Layout(UICollection​View​Layout, animated:​ Bool, completion:​ ((Bool) -> Void)? = nil)

Changes the collection view’s layout and notifies you when the animations complete.

func finish​Interactive​Transition()

Tells the collection view to finish an interactive transition by installing the intended target layout.

func cancel​Interactive​Transition()

Tells the collection view to abort an interactive transition and return to its original layout object.

Reloading Content

func reload​Data()

Reloads all of the data for the collection view.

func reload​Sections(Index​Set)

Reloads the data in the specified sections of the collection view.

func reload​Items(at:​ [Index​Path])

Reloads just the items at the specified index paths.

Getting the State of the Collection View

var number​Of​Sections:​ Int

Returns the number of sections displayed by the collection view.

func number​Of​Items(in​Section:​ Int)

Returns the number of items in the specified section.

var visible​Cells:​ [UICollection​View​Cell]

Returns an array of visible cells currently displayed by the collection view.

Inserting, Moving, and Deleting Items

func insert​Items(at:​ [Index​Path])

Inserts new items at the specified index paths.

func move​Item(at:​ Index​Path, to:​ Index​Path)

Moves an item from one location to another in the collection view.

func delete​Items(at:​ [Index​Path])

Deletes the items at the specified index paths.

Inserting, Moving, and Deleting Sections

func insert​Sections(Index​Set)

Inserts new sections at the specified indexes.

func move​Section(Int, to​Section:​ Int)

Moves a section from one location to another in the collection view.

func delete​Sections(Index​Set)

Deletes the sections at the specified indexes.

Reordering Items Interactively

func begin​Interactive​Movement​For​Item(at:​ Index​Path)

Initiates the interactive movement of the item at the specified index path.

func update​Interactive​Movement​Target​Position(CGPoint)

Updates the position of the item within the collection view’s bounds.

func end​Interactive​Movement()

Ends interactive movement tracking and moves the target item to its new location.

func cancel​Interactive​Movement()

Ends interactive movement tracking and returns the target item to its original location.

Managing the Selection

var allows​Selection:​ Bool

A Boolean value that indicates whether users can select items in the collection view.

var allows​Multiple​Selection:​ Bool

A Boolean value that determines whether users can select more than one item in the collection view.

func select​Item(at:​ Index​Path?, animated:​ Bool, scroll​Position:​ UICollection​View​Scroll​Position)

Selects the item at the specified index path and optionally scrolls it into view.

func deselect​Item(at:​ Index​Path, animated:​ Bool)

Deselects the item at the specified index.

Managing Focus

var remembers​Last​Focused​Index​Path:​ Bool

A Boolean value indicating whether the collection view automatically assigns the focus to the item at the last focused index path.

Locating Items and Views in the Collection View

func index​Path​For​Item(at:​ CGPoint)

Returns the index path of the item at the specified point in the collection view.

var index​Paths​For​Visible​Items:​ [Index​Path]

An array of the visible items in the collection view.

func index​Path(for:​ UICollection​View​Cell)

Returns the index path of the specified cell.

func cell​For​Item(at:​ Index​Path)

Returns the visible cell object at the specified index path.

func index​Paths​For​Visible​Supplementary​Elements(of​Kind:​ String)

Returns the index paths of all visible supplementary views of the specified type.

func supplementary​View(for​Element​Kind:​ String, at:​ Index​Path)

Returns the supplementary view at the specified index path.

func visible​Supplementary​Views(of​Kind:​ String)

Returns an array of the visible supplementary views of the specified kind.

Getting Layout Information

func layout​Attributes​For​Item(at:​ Index​Path)

Returns the layout information for the item at the specified index path.

func layout​Attributes​For​Supplementary​Element(of​Kind:​ String, at:​ Index​Path)

Returns the layout information for the specified supplementary view.

Scrolling an Item Into View

func scroll​To​Item(at:​ Index​Path, at:​ UICollection​View​Scroll​Position, animated:​ Bool)

Scrolls the collection view contents until the specified item is visible.

Animating Multiple Changes to the Collection View

func perform​Batch​Updates((() -> Void)?, completion:​ ((Bool) -> Void)? = nil)

Animates multiple insert, delete, reload, and move operations as a group.



Constants that indicate how to scroll an item into the visible portion of the collection view.


The completion block called at the end of an interactive transition for a collection view.


Inherits From