Last active
September 5, 2019 08:25
-
-
Save f-meloni/662228c0e164c42b6d58fb2d55308812 to your computer and use it in GitHub Desktop.
Cells reusableIdentifier and nib utility extension
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 Foundation | |
import UIKit | |
public protocol Reusable: class, NSObjectProtocol { | |
static var reusableIdentifier: String { get } | |
} | |
public extension Reusable { | |
public static var reusableIdentifier: String { return String(Self) } | |
} | |
public protocol Nibable: class, NSObjectProtocol { | |
static var nib: UINib { get } | |
static var nibName: String { get } | |
} | |
public extension Nibable { | |
public static var nib: UINib { return UINib(nibName: String(Self), bundle: NSBundle(forClass: self)) } | |
public static var nibName: String { return String(Self) } | |
public static func fromNib<T : UIView>() -> T? { return UINib(nibName: String(Self), bundle: NSBundle(forClass: self)).instantiateWithOwner(nil, options: nil).flatMap { $0 as? T }.first } | |
} | |
extension UIView: Nibable { } | |
extension UIViewController: Nibable { } | |
extension UITableViewCell: Reusable { } | |
extension UITableViewHeaderFooterView : Reusable { } | |
extension UICollectionReusableView: Reusable { } |
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 Foundation | |
import UIKit | |
public protocol Reusable: class, NSObjectProtocol { | |
static var reusableIdentifier: String { get } | |
} | |
public extension Reusable { | |
public static var reusableIdentifier: String { | |
return String(describing: Self.self) | |
} | |
} | |
public protocol Nibable: class, NSObjectProtocol { | |
static var nib: UINib { get } | |
static var nibName: String { get } | |
} | |
public extension Nibable { | |
public static var nib: UINib { return UINib(nibName: String(describing: Self.self), bundle: Bundle(for: self)) } | |
public static var nibName: String { return String(describing: Self.self) } | |
} | |
extension UITableViewCell: Reusable, Nibable { } | |
extension UICollectionReusableView: Reusable, Nibable { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment