Skip to content

Instantly share code, notes, and snippets.

@bartjacobs
Created August 22, 2017 05:34
Show Gist options
  • Save bartjacobs/50864fa7b10fb6298e91c5867235b6b5 to your computer and use it in GitHub Desktop.
Save bartjacobs/50864fa7b10fb6298e91c5867235b6b5 to your computer and use it in GitHub Desktop.
import UIKit
protocol ReusableView {
static var reuseIdentifier: String { get }
}
extension ReusableView {
static var reuseIdentifier: String {
return String(describing: self)
}
}
extension UITableViewCell: ReusableView {
}
extension UICollectionViewCell: ReusableView {
}
extension UITableView {
func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T {
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
fatalError("Unable to Dequeue Reusable Table View Cell")
}
return cell
}
}
extension UICollectionView {
func dequeueReusableCell<T: UICollectionViewCell>(for indexPath: IndexPath) -> T {
guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
fatalError("Unable to Dequeue Reusable Collection View Cell")
}
return cell
}
}
// Dequeue Table View Cell
let cell: ValueTableViewCell = tableView.dequeueReusableCell(for: indexPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment