A type safe way to auto dequeue UITableView, UICollectionView, and MKMapView elements without having to call the register function. Element registration, type casting, and error handling are all done inside a the single dequeue method.
- iOS 9.0
- Xcode 10.2
- Swift 5.0
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.2.0'
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.2.0")
]
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
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
}
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
}
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
}
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
}
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
}
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