Last active
November 3, 2019 12:48
-
-
Save NeilsUltimateLab/242b527d489e3525df54be5bd8630aa4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
protocol ReusableView: class { | |
static var identifier: String { get } | |
} | |
extension ReusableView where Self: NSObject { | |
static var identifier: String { | |
return String(describing: self) | |
} | |
} | |
extension UITableViewCell: ReusableView {} | |
extension UITableViewHeaderFooterView: ReusableView {} | |
extension UICollectionReusableView : ReusableView {} | |
extension UIViewController: ReusableView {} | |
protocol NibLoadableView: class { | |
static var nibName: String { get } | |
} | |
extension NibLoadableView where Self: UIView { | |
static var nibName: String { | |
return String(describing: self) | |
} | |
static func initFromNib() -> Self? { | |
guard let view = (Bundle.main.loadNibNamed(nibName, owner: self, options: nil) as? [Self])?.first else { | |
return nil | |
} | |
return view | |
} | |
} | |
extension UIView: NibLoadableView {} | |
extension UITableView { | |
func register<T: UITableViewCell>(_ : T.Type) { | |
self.register(T.self, forCellReuseIdentifier: T.identifier) | |
} | |
func registerNib<T: UITableViewCell>(_ : T.Type) { | |
let nib = UINib(nibName: T.nibName, bundle: nil) | |
self.register(nib, forCellReuseIdentifier: T.identifier) | |
} | |
func register<T: UITableViewHeaderFooterView>(_ : T.Type) { | |
self.register(T.self, forHeaderFooterViewReuseIdentifier: T.identifier) | |
} | |
func registerNib<T: UITableViewHeaderFooterView>(_ : T.Type) { | |
let nib = UINib(nibName: T.nibName, bundle: nil) | |
self.register(nib, forHeaderFooterViewReuseIdentifier: T.identifier) | |
} | |
func dequeueReusableCell<T: UITableViewCell>(_ : T.Type, for indexPath: IndexPath) -> T? { | |
return dequeueReusableCell(withIdentifier: T.identifier, for: indexPath) as? T | |
} | |
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(_ : T.Type) -> T? { | |
return dequeueReusableHeaderFooterView(withIdentifier: T.identifier) as? T | |
} | |
} | |
extension UICollectionView { | |
func register<T: UICollectionReusableView>(_ : T.Type) { | |
register(T.self, forCellWithReuseIdentifier: T.identifier) | |
} | |
func register<T: UICollectionReusableView>(_ : T.Type, ofKind kind: String) { | |
register(T.self, forSupplementaryViewOfKind: kind, withReuseIdentifier: T.identifier) | |
} | |
func registerNib<T: UICollectionReusableView>(_ : T.Type) { | |
let reuseIdentifer = T.identifier | |
let nib = UINib(nibName: T.nibName, bundle: nil) | |
register(nib, forCellWithReuseIdentifier: reuseIdentifer) | |
} | |
func registerNib<T: UICollectionReusableView>(_ : T.Type, ofKind kind: String) { | |
let nib = UINib(nibName: T.nibName, bundle: nil) | |
register(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: T.identifier) | |
} | |
func dequeueReusableCell<T: UICollectionViewCell>(_ : T.Type, for indexPath: IndexPath) -> T? { | |
return dequeueReusableCell(withReuseIdentifier: T.identifier, | |
for: indexPath) as? T | |
} | |
func dequeueReusableSupplementaryView<T: UICollectionViewCell>(_ : T.Type, ofKind kind: String, for indexPath: IndexPath) -> T? { | |
return dequeueReusableSupplementaryView(ofKind: kind, | |
withReuseIdentifier: T.identifier, | |
for: indexPath) as? T | |
} | |
} |
This file contains hidden or 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 | |
struct Row<Value, Cell: ReusableView> { | |
var value: Value | |
var cellType: Cell.Type | |
init(_ value: Value) { | |
self.value = value | |
self.cellType = Cell.self | |
} | |
} | |
// MARK: - Row extension for the tableViewCell subclasses | |
extension Row where Cell: UITableViewCell { | |
func registerCell(in tableView: UITableView) { | |
tableView.register(Cell.self) | |
} | |
func registerNib(in tableView: UITableView) { | |
tableView.registerNib(Cell.self) | |
} | |
func dequeueReusableCell(from tableView: UITableView, at indexPath: IndexPath) -> Cell? { | |
let cell = tableView.dequeueReusableCell(Cell.self, for: indexPath) | |
return cell | |
} | |
} | |
This file contains hidden or 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
enum Storyboard: String { | |
case main = "Main" | |
} | |
protocol StoryboardInstantiable { | |
static var storyboardIdentifier: String { get } | |
static func instantiate(from storyboard: Storyboard) -> Self | |
static func instantiateInitialViewController(from storyboard: Storyboard) -> Self | |
} | |
extension StoryboardInstantiable where Self: UIViewController { | |
static var storyboardIdentifier: String { | |
return String(describing: self) | |
} | |
static func instantiate(from storyboard: Storyboard) -> Self { | |
return UIStoryboard.storyboard(storyboard: storyboard).instantiateViewController() | |
} | |
static func instantiateInitialViewController(from storyboard: Storyboard) -> Self { | |
return UIStoryboard.storyboard(storyboard: storyboard).instantiateInitialViewController() | |
} | |
} | |
extension UIViewController: StoryboardInstantiable { } | |
extension UIStoryboard { | |
convenience init(storyboard: Storyboard, bundle: Bundle? = nil) { | |
self.init(name: storyboard.rawValue, bundle: bundle) | |
} | |
class func storyboard(storyboard: Storyboard, bundle: Bundle? = nil) -> UIStoryboard { | |
return UIStoryboard(name: storyboard.rawValue, bundle: bundle) | |
} | |
func instantiateViewController<T: UIViewController>() -> T { | |
guard let viewController = instantiateViewController(withIdentifier: T.storyboardIdentifier) as? T else { | |
fatalError("Could not find view controller with name \(T.storyboardIdentifier)") | |
} | |
return viewController | |
} | |
func instantiateInitialViewController<T: UIViewController>() -> T { | |
guard let viewController = instantiateInitialViewController() as? T else { | |
fatalError("Could not find initial view controller in storyboard: \(self.description)") | |
} | |
return viewController | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment