UICollectionView

extension UICollectionView
  • Returns a UICollectionView with the specified layout.

    Declaration

    Swift

    public convenience init(layout: UICollectionViewFlowLayout)
  • Registers a UICollectionViewCell for use in a UICollectionView.

    By default, the class name of the cell is used as the reuse identifier.

    Example:

    class CustomCell: UICollectionViewCell {}
    
    let collectionView = UICollectionView()
    
    // registers the CustomCell class with a reuse identifier of "CustomCell"
    collectionView.registerCell(CustomCell)
    

    Declaration

    Swift

    public func registerCell<T>(_ type: T.Type, withIdentifier reuseIdentifier: String = String(describing: T.self)) where T : UICollectionViewCell

    Parameters

    type

    The type of cell to register.

    reuseIdentifier

    The reuse identifier for the cell (optional).

  • Dequeues a UICollectionViewCell for use in a UICollectionView.

    By default, the class name of the cell is used as the reuse identifier.

    Example:

    class CustomCell: UICollectionViewCell {}
    
    let collectionView = UICollectionView()
    
    // dequeues a CustomCell class
    let cell = collectionView.dequeueReusableCell(CustomCell.self, forIndexPath: indexPath)
    

    Declaration

    Swift

    public func dequeueCell<T: UICollectionViewCell>(_ type: T.Type = T.self,
                             withIdentifier reuseIdentifier: String = String(describing: T.self),
                                              for indexPath: IndexPath) -> T

    Parameters

    type

    The type of the cell.

    indexPath

    The index path at which to dequeue a new cell.

    reuseIdentifier

    The reuse identifier for the cell (optional).

    Return Value

    A force-casted UICollectionViewCell of the specified type.

  • Registers a UICollectionReusableView for use in a UICollectionView section header.

    By default, the class name of the header view is used as the reuse identifier.

    Example:

    class CustomHeader: UICollectionReusableView {}
    
    let collectionView = UICollectionView()
    
    // registers the CustomCell class with a reuse identifier of "CustomHeader"
    collectionView.registerHeader(CustomHeader)
    

    Declaration

    Swift

    public func registerHeader<T>(_ type: T.Type, withIdentifier reuseIdentifier: String = String(describing: T.self)) where T : UICollectionReusableView

    Parameters

    type

    The type of header view to register.

    reuseIdentifier

    The reuse identifier for the header view (optional).

  • Dequeues a UICollectionReusableView for use in a UICollectionView section header.

    By default, the class name of the header view is used as the reuse identifier.

    Example:

    class CustomHeader: UICollectionReusableView {}
    
    let collectionView = UICollectionView()
    
    // dequeues a CustomHeader class
    let footerView = collectionView.dequeueReusableHeader(CustomHeader.self, forIndexPath: indexPath)
    

    Declaration

    Swift

    public func dequeueHeader<T: UICollectionReusableView>(_ type: T.Type = T.self,
                                   withIdentifier reuseIdentifier: String = String(describing: T.self),
                                                    for indexPath: IndexPath) -> T

    Parameters

    type

    The type of the header view.

    indexPath

    The index path at which to dequeue a new header view.

    reuseIdentifier

    The reuse identifier for the header view (optional).

    Return Value

    A force-casted UICollectionReusableView of the specified type.

  • Registers a UICollectionReusableView for use in a UICollectionView section footer.

    By default, the class name of the footer view is used as the reuse identifier.

    Example:

    class CustomFooter: UICollectionReusableView {}
    
    let collectionView = UICollectionView()
    
    // registers the CustomFooter class with a reuse identifier of "CustomFooter"
    collectionView.registerFooter(CustomFooter)
    

    Declaration

    Swift

    public func registerFooter<T>(_ type: T.Type, withIdentifier reuseIdentifier: String = String(describing: T.self)) where T : UICollectionReusableView

    Parameters

    type

    The type of footer view to register.

    reuseIdentifier

    The reuse identifier for the footer view (optional).

  • Dequeues a UICollectionReusableView for use in a UICollectionView section footer.

    By default, the class name of the footer view is used as the reuse identifier.

    Example:

    class CustomFooter: UICollectionReusableView {}
    
    let collectionView = UICollectionView()
    
    // dequeues a CustomFooter class
    let footerView = collectionView.dequeueReusableFooter(CustomFooter.self, forIndexPath: indexPath)
    

    Declaration

    Swift

    public func dequeueFooter<T: UICollectionReusableView>(_ type: T.Type = T.self,
                                   withIdentifier reuseIdentifier: String = String(describing: T.self),
                                                    for indexPath: IndexPath) -> T

    Parameters

    type

    The type of the footer view.

    indexPath

    The index path at which to dequeue a new footer view.

    reuseIdentifier

    The reuse identifier for the footer view (optional).

    Return Value

    A force-casted UICollectionReusableView of the specified type.

  • Inserts rows into self.

    Declaration

    Swift

    public func insert(_ indices: [Int], section: Int = 0, completion: @escaping (Bool) -> Void = { _ in })

    Parameters

    indices

    The rows indices to insert into self.

    section

    The section in which to insert the rows (optional, defaults to 0).

    completion

    The completion handler, called after the rows have been inserted (optional).

  • Deletes rows from self.

    Declaration

    Swift

    public func delete(_ indices: [Int], section: Int = 0, completion: @escaping (Bool) -> Void = { _ in })

    Parameters

    indices

    The rows indices to delete from self.

    section

    The section in which to delete the rows (optional, defaults to 0).

    completion

    The completion handler, called after the rows have been deleted (optional).

  • Reloads rows in self.

    Declaration

    Swift

    public func reload(_ indices: [Int], section: Int = 0, completion: @escaping (Bool) -> Void = { _ in })

    Parameters

    indices

    The rows indices to reload in self.

    section

    The section in which to reload the rows (optional, defaults to 0).

    completion

    The completion handler, called after the rows have been reloaded (optional).

  • The minimum (starting) IndexPath for traversing a UICollectionView sequentially.

    Declaration

    Swift

    public var minimumIndexPath: IndexPath { get }
  • The maximum (ending) IndexPath for traversing a UICollectionView sequentially.

    Declaration

    Swift

    public var maximumIndexPath: IndexPath { get }
  • When sequentially traversing a UICollectionView, what’s the next IndexPath after the given IndexPath.

    See also

    var maximumIndexpath

    Declaration

    Swift

    public func indexPath(after indexPath: IndexPath) -> IndexPath?

    Parameters

    indexPath

    The current indexPath; the path we want to find what comes after.

    Return Value

    The next indexPath, or nil if we’re at the maximumIndexPath

  • When sequentially traversing a UICollectionView, what’s the previous IndexPath before the given IndexPath.

    See also

    var minimumIndexPath

    Declaration

    Swift

    public func indexPath(before indexPath: IndexPath) -> IndexPath?

    Parameters

    indexPath

    The current indexPath; the path we want to find what comes before.

    Return Value

    The prior indexPath, or nil if we’re at the minimumIndexPath