Skip to content
A type safe way to dequeue UITableView, UICollectionView, and MKMapView elements without having to call register.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
AutoDequeue.xcodeproj
AutoDequeue
AutoDequeueTests
.gitignore
AutoDequeue.podspec
LICENSE
README.md

README.md

AutoDequeue

Swift CocoaPods

A type safe way to dequeue UITableView, UICollectionView, and MKMapView elements without having to call register. Element registration, type casting, and error handling are all done inside a the single dequeue method for each view.

Requirements

  • iOS 9.0+
  • Xcode 10.2+
  • Swift 5.0+

Instalation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate the library into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'AutoDequeue', '1.1.0'

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

dependencies: [
    .package(url: "https://github.com/berbschloe/AutoDequeue.git", from: "1.1.0")
]

Usage

Importing

It would be recommended to add AutoDequeue globally because it can get annoying importing it everywhere.

// Add this to a GlobalImports.swift
@_exported import AutoDequeue

How to use for UITableView

Old Way

func viewDidLoad() {
    super.viewDidLoad()
    // register elements by a reuse id
    tableView.register(CustomCell.self, forCellReuseIdentifier: "CELL_ID")
    tableView.register(CustomHeader.self, forHeaderFooterViewReuseIdentifier: "HEADER_ID")
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_ID", for: indexPath) as? CustomCell else {
        fatalError("Invalid Class")
    }
    
    // setup cell
    
    return cell
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    guard let header = dequeueReusableHeaderFooterView(withIdentifier: "HEADER_ID") as? CustomHeader else {
        fatalError("Invalid Class")
    }
    
    // setup header
    
    return header
}

AutoDequeue Way

func viewDidLoad() {
    super.viewDidLoad() 
    // no need to register elements
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: CustomCell = tableView.dequeueReusableCell(for: indexPath)
    
    // setup cell
    
    return cell
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header: CustomHeader = dequeueReusableHeaderFooterView()
    
    // setup header
    
    return header
}

How to use for UICollectionView

Old Way

func viewDidLoad() {
    super.viewDidLoad()
    // register elements by a reuse id
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "CELL_ID")
    collectionView.register(CustomHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HEADER_ID")
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = dequeueReusableCell(withReuseIdentifier: "CELL_ID", for: indexPath) as? CustomCell else {
        fatalError("Invalid Class")
    }
    
    // setup cell
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
                    
     guard let header = dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HEADER_ID", for: indexPath
       ) as? CustomHeader else {
            fatalError("Invalid Class")
       }
       
       // setup header
       
       return header
}

AutoDequeue Way

func viewDidLoad() {
    super.viewDidLoad()
    // no need to register elements
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: CustomCell = dequeueReusableCell(for: indexPath)
    
    // setup cell
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header: CustomHeader = dequeueReusableSupplementaryView(for: indexPath)
       
    // setup header
       
    return header
}

How to use for MKMapView

Old Way

func viewDidLoad() {
    super.viewDidLoad()
    // register elements by a reuse id
    mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: "ANNOTATION_ID")
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "ANNOTATION_ID", for: annotation) as? CustomAnnotationView else {
        fatalError("Invalid Class")
    }
    
    return annotationView
}

AutoDequeue

func viewDidLoad() {
    super.viewDidLoad()
    // no need to register elements
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView: CustomAnnotationView = mapView.dequeueReusableAnnotationView(for annotation)
    return annotationView
You can’t perform that action at this time.