Collection View
-
A Collection View manages the presentation of items, your app’s main job is to provide the data that those items are to represent.
A collection view gets its data from the data source, which is an object that conforms to the CollectionViewDataSource protocol. Data provided by the data source is represented items, which can also be organized into sections, and ultimately displayed as cells.
Gathings Data
The data source only has 3 requirements, provide the number of sections, the number of items, and a cell for each item. For performance, cells in a collection view are reusable since only a subset of all the items will often be visbile.
Before you can load a cell, you need to register the cells you will need to represet your data. Cells can be registered from a nib or from a class using
register(class:forCellWithReuseIdentifier:)
orregister(nib:forCellWithReuseIdentifier:)
. The reuse identifier will later be use to get instances of the cell.To create the cells for each item in your data, implement the data source method
func collectionView(_:cellForItemAt:) -> CollectionViewCell
. In here you will calldequeueReusableCell(withReuseIdentifier:for:)
which will load an instance of the cell your previously registered for that resuse identifier. After you have dequeued the cell, update it as needed to properly represent the object at the given index path in your data, then return it.Laying Out Items
After the data source has provided all the cells to be displayed, the collection view looks to its CollectionViewLayout to determine where to place each one. The base layout object is designed to be subclassed to generate layout information for different use cases. The goal of a layout is to be able to provide information about the layout to the collection view quickly, this inlcudes the location of each cell, the overall size of all the items, etc.
The following layouts are provided for common uses, for more custom layouts, create a custom CollectionViewLayout subclass.
- CollectionViewColumnLayout
- CollectionViewListLayout
- CollectionViewFlowLayout
- CollectionViewHorizontalLayout
Declaration
Swift
open class CollectionView : ScrollView, NSDraggingSource
-
The CollectionViewDataSource is responsible for providing the data and views required by a collection view
Overview
At a minimum, all data source objects must implement:
numberOfSections(in:)
collectionView(_:numberOfItemsInSection:)
collectionView(_:cellForItemAt:)
.
These methods are responsible for returning the number of items in the collection view along with the items themselves.
See moreDeclaration
Swift
@objc public protocol CollectionViewDataSource
-
The CollectionViewDelegate protocol defines methods that allow you to manage the status, selection, highlighting, and scrolling of items in a collection view and to perform actions on those items. The methods of this protocol are all optional.
See moreDeclaration
Swift
@objc public protocol CollectionViewDelegate
-
The CollectionViewDragDelegate forwards system drag functions to the delegate in the context of a Collection View.
See moreDeclaration
Swift
@objc public protocol CollectionViewDragDelegate : CollectionViewDelegate
-
A CollectionViewCell object presents the content for a single data item when that item is within the collection view’s visible bounds. You can use this class as-is or subclass it to add additional properties and methods. The layout and presentation of cells is managed by the collection view and its corresponding layout object.
See moreDeclaration
Swift
open class CollectionViewCell : CollectionReusableView
-
The CollectionReusableView class defines the behavior for all cells and supplementary views presented by a collection view. Reusable views are so named because the collection view places them on a reuse queue rather than deleting them when they are scrolled out of the visible bounds. Such a view can then be retrieved and repurposed for a different set of content.
See moreDeclaration
Swift
open class CollectionReusableView : NSView
-
The UICollectionViewController class represents a view controller whose content consists of a collection view.
See moreDeclaration
Swift
open class CollectionViewController : NSViewController, CollectionViewDataSource, CollectionViewDelegate
-
CollectionViewLayoutElementKind
Declaration
Swift
public struct CollectionViewLayoutElementKind
-
CollectionElementCategory
- cell:
- supplementaryView:
Declaration
Swift
public enum CollectionElementCategory
-
CollectionViewDirection
- left:
- right:
- up:
- down:
Declaration
Swift
public enum CollectionViewDirection
-
CollectionViewScrollDirection
- vertical:
- horizontal:
Declaration
Swift
public enum CollectionViewScrollDirection
-
CollectionViewScrollPosition
- none:
- nearest:
- leading:
- centered:
- trailing:
Declaration
Swift
public enum CollectionViewScrollPosition
-
AnimationCompletion
Declaration
Swift
public typealias AnimationCompletion = (_ finished: Bool) -> Void
-
A default implementation of CollectionViewPreviewTransitionCell
See moreDeclaration
Swift
open class CollectionViewPreviewCell : CollectionViewCell, CollectionViewPreviewTransitionCell
-
A protocol for CollectionViewCells that need to customize their transition when used in CollectionViewPreviewController.
If you adopt this protocol in a custom CollectionViewCell subclass, see the source code for CollectionViewPreviewCell for an example implementation.
See moreDeclaration
Swift
public protocol CollectionViewPreviewTransitionCell : AnyObject
-
An easy to use CollectionViewController that transitions from a source collection view.
Presentation & Data
The controller is presented from a source collection view. The data source of the source collection view is used to load data for the preview collection view. The preview controller will act as a proxy between the preview collection view and your source colleciton views data source.
Important
The data source for the collection view passed to present(in:) must conform to CollectionViewPreviewControllerDelegateTransitions
The preview controller manages the transitions to and from the source and allows the preview cell to customize the transition.
Although the The preview controller will accept any cell class, supporting transitions requires a small amount of additional setup.
The simplest way to support transitions is to create your subclass from CollectionViewPreviewCell. CollectionViewPreviewCell will animate the frame of the cell from the source and back.
For custom transitions, if you subclass CollectionViewPreviewCell you can simply override the the transition methods, otherwise conform your CollectionViewCell subclass and implement the methods yourself. See CollectionViewPreviewTransitionCell for more about how to implement custom transitions
See moreDeclaration
Swift
open class CollectionViewPreviewController : CollectionViewController, CollectionViewDelegatePreviewLayout
-
a CollectionViewPreviewControllerDelegate is responsible for providing data to a CollectionViewPreviewController.
See moreDeclaration
Swift
public protocol CollectionViewPreviewControllerDelegate : AnyObject