Skip to content

Instantly share code, notes, and snippets.

@rsaenzi
Last active November 23, 2020 15:18
Show Gist options
  • Save rsaenzi/9df6cb80fe4828d89aa664de03b5f25f to your computer and use it in GitHub Desktop.
Save rsaenzi/9df6cb80fe4828d89aa664de03b5f25f to your computer and use it in GitHub Desktop.
To load a UITableViewCell or UICollectionViewCell by the class name, avoiding ! operator and using a string literal for the name of the cell identifier Raw
//
// UITableView+Load.swift
//
// Created by Rigoberto Sáenz Imbacuán (https://www.linkedin.com/in/rsaenzi/)
// Copyright © 2019. All rights reserved.
//
import UIKit
extension UITableView {
/**
Registers a UITableViewCell on this UITableView using the UITableViewCell class name as Reuse Identifier
Usage: table.registerCell(CellClass.self)
*/
func registerCell<T: UITableViewCell>(_ cell: T.Type) {
register(cell, forCellReuseIdentifier: cell.getReuseIdentifier())
}
func registerHeader<T: UITableViewHeaderFooterView>(_ cell: T.Type) {
register(cell, forHeaderFooterViewReuseIdentifier: cell.getReuseIdentifier())
}
/**
Dequeues a UITableViewCell from current UITableView only if the UITableViewCell class and the cell Identifier are the same
Usage: let cell: CellClass = table.dequeue(indexPath)
*/
func dequeueCell<T: UITableViewCell>(_ indexPath: IndexPath) -> T {
let identifier = className(some: T.self)
let rawCell = self.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
guard let cell = rawCell as? T else {
fatalError("UITableViewCell with identifier '\(identifier)' was not found")
//return T(frame: .zero)
}
return cell
}
func dequeueHeader<T: UITableViewHeaderFooterView>() -> T {
let identifier = className(some: T.self)
let rawHeader = self.dequeueReusableHeaderFooterView(withIdentifier: identifier)
guard let header = rawHeader as? T else {
fatalError("UITableViewHeaderFooterView with identifier '\(identifier)' was not found")
//return T(frame: .zero)
}
return header
}
}
extension UICollectionView {
/**
Registers a UICollectionViewCell on this UICollectionView using the UICollectionViewCell class name as Reuse Identifier
*/
func registerCell<T: UICollectionViewCell>(_ cell: T.Type) {
register(cell, forCellWithReuseIdentifier: T.getReuseIdentifier())
}
/**
Dequeues a UICollectionViewCell from current UICollectionView only if the UICollectionViewCell class and the cell Identifier are the same
*/
func dequeueCell<T: UICollectionViewCell>(_ indexPath: IndexPath) -> T {
let identifier = className(some: T.self)
let rawCell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
guard let cell = rawCell as? T else {
fatalError("UICollectionViewCell with identifier '\(identifier)' was not found")
//return T(frame: .zero)
}
return cell
}
}
extension UITableViewCell {
fileprivate static func getReuseIdentifier() -> String {
return className(some: self)
}
}
extension UITableViewHeaderFooterView {
fileprivate static func getReuseIdentifier() -> String {
return className(some: self)
}
}
extension UICollectionViewCell {
fileprivate static func getReuseIdentifier() -> String {
return className(some: self)
}
}
private func className(some: Any) -> String {
return (some is Any.Type) ? "\(some)" : "\(type(of: some))"
}
@rsaenzi
Copy link
Author

rsaenzi commented Mar 12, 2020

@rsaenzi
Copy link
Author

rsaenzi commented Mar 12, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment