Created
August 22, 2017 05:34
-
-
Save bartjacobs/50864fa7b10fb6298e91c5867235b6b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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