Skip to content

Latest commit

 

History

History
251 lines (172 loc) · 7.37 KB

UICollectionViewExtensions.md

File metadata and controls

251 lines (172 loc) · 7.37 KB

UICollectionView Extensions

Table of Contents

Name Type iOS tvOS watchOS macOS
indexPathForLastItem Read-Only Property 8+ 9+ - -
lastSection Read-Only Property 8+ 9+ - -
numberOfItems Read-Only Property 8+ 9+ - -
indexPathForLastItem(inSection section: Int) Method 8+ 9+ - -
reloadData(_ completion: () -> Void) Method 8+ 9+ - -
dequeueReusableCell <T: UICollectionViewCell> (withClass name: T.Type, for indexPath: IndexPath) Method 8+ 9+ - -
dequeueReusableSupplementaryView <T: UICollectionReusableView> (ofKind: String, withClass name: T.Type, for indexPath: IndexPath) Method 8+ 9+ - -
register <T: UICollectionReusableView> (supplementaryViewOfKind kind: String, withClass name: T.Type) Method 8+ 9+ - -
register <T: UICollectionViewCell> (nib: UINib?, forCellWithClass name: T.Type) Method 8+ 9+ - -
register <T: UICollectionViewCell> (cellWithClass name: T.Type) Method 8+ 9+ - -
register <T: UICollectionReusableView> (nib: UINib?, forSupplementaryViewOfKind kind: String, withClass name: T.Type) Method 8+ 9+ - -

indexPathForLastItem

Index path of last item in collectionView.

  • type: Read-Only Property.
  • return type: IndexPath
  • availability: iOS 8+, tvOS 9+.
  • unit tests: iOS, tvOS.

Example

let collectionView = UICollectionView()
let indexPath = collectionView.indexPathForLastItem

--

lastSection

Index of last section in collectionView.

  • type: Read-Only Property.
  • return type: Int
  • availability: iOS 8+, tvOS 9+.
  • unit tests: iOS, tvOS.

Example

let collectionView = UICollectionView()
let lastSection = collectionView.lastSection

numberOfItems

Number of all items in all sections of collectionView.

  • type: Read-Only Property.
  • return type: Int
  • availability: iOS 8+, tvOS 9+.
  • unit tests: iOS, tvOS.

Example

let collectionView = UICollectionView()
let itemCount = collectionView.numberOfItems

indexPathForLastItem(inSection section: Int)

IndexPath for last item in section.

  • type: Method
  • parameters:
    • section: section to get last item in.
  • return type: IndexPath?
  • availability: iOS 8+, tvOS 9+.
  • unit tests: iOS, tvOS.

Example

let collectionView = UICollectionView()
let indexPath = collectionView.indexPathForLastItem(section: 0)

reloadData(_ completion: () -> Void)

Reload data with a completion handler.

  • type: Method
  • parameters:
    • completion: completion handler to run after reloadData finishes.
  • return type: Void
  • availability: iOS 8+, tvOS 9+.

Example

let collectionView = UICollectionView()
collectionView.reloadData {
   print("SwifterSwift: Data has completed reloading")
}

dequeueReusableCell <T: UICollectionViewCell> (withClass name: T.Type, for indexPath: IndexPath)

Dequeue reusable UICollectionViewCell using class name.

  • type: Method
  • parameters:
    • name: UICollectionViewCell type.
    • indexPath: Location of cell in collectionView.
  • returns: UICollectionViewCell object with associated class name.
  • return type: T (must be type of UICollectionViewCell)
  • availability: iOS 8+, tvOS 9+.

Example

let collectionView = UICollectionView()
let indexPath = IndexPath()
let cell = collectionView.dequeueReusableCell(withClass: UICollectionViewCell.self, for: indexPath)

dequeueReusableSupplementaryView <T: UICollectionReusableView> (ofKind: String, withClass name: T.Type, for indexPath: IndexPath)

Dequeue reusable UICollectionReusableView using class name.

  • type: Method
  • parameters:
    • ofKind: The kind of supplementary view to retrieve. This value is defined by the layout object.
    • name: UICollectionReusableView type.
    • indexPath: Location of cell in collectionView.
  • returns: UICollectionReusableView object with associated class name.
  • return type: T (must be type of UICollectionReusableView)
  • availability: iOS 8+, tvOS 9+.

Example

let collectionView = UICollectionView()
let indexPath = IndexPath()
let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: "UICollectionReusableView", withClass: UICollectionReusableView.self, for: indexPath)

register <T: UICollectionReusableView> (supplementaryViewOfKind kind: String, withClass name: T.Type)

Register UICollectionReusableView using class name.

  • type: Method
  • parameters:
    • kind: The kind of supplementary view to retrieve. This value is defined by the layout object.
    • name: UICollectionReusableView type
  • return type: Void
  • availability: iOS 8+, tvOS 9+.

Example

let collectionView = UICollectionView()
collectionView.register(supplementaryViewOfKind: "UICollectionReusableView", withClass: UICollectionReusableView.self)

register <T: UICollectionViewCell> (nib: UINib?, forCellWithClass name: T.Type)

Register UICollectionViewCell using class name.

  • type: Method
  • parameters:
    • nib: Nib file used to create the collectionView cell.
    • name: UICollectionViewCell type.
  • return type: Void
  • availability: iOS 8+, tvOS 9+.

Example

let collectionView = UICollectionView()
let nib = UINib()
collectionView.register(nib: nib, forCellWithClass: UICollectionViewCell.self)

register <T: UICollectionViewCell> (cellWithClass name: T.Type)

Register UICollectionViewCell using class name.

  • type: Method
  • parameters:
    • name: UICollectionViewCell type.
  • return type: Void
  • availability: iOS 8+, tvOS 9+.

Example

let collectionView = UICollectionView()
collectionView.register(cellWithClass: UICollectionViewCell.self)

register <T: UICollectionReusableView> (nib: UINib?, forSupplementaryViewOfKind kind: String, withClass name: T.Type)

Register UICollectionReusableView using class name.

  • type: Method
  • parameters:
    • nib: Nib file used to create the reusable view.
    • kind: The kind of supplementary view to retrieve. This value is defined by the layout object.
    • name: UICollectionReusableView type.
  • return type: Void
  • availability: iOS 8+, tvOS 9+.

Example

let collectionView = UICollectionView()
let nib = UINib()
collectionView.register(nib: nib, forSupplementaryViewOfKind: "UICollectionReusableView", withClass: UICollectionReusableView.self)