Skip to content

Instantly share code, notes, and snippets.

@f-meloni
Last active September 5, 2019 08:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save f-meloni/662228c0e164c42b6d58fb2d55308812 to your computer and use it in GitHub Desktop.
Save f-meloni/662228c0e164c42b6d58fb2d55308812 to your computer and use it in GitHub Desktop.
Cells reusableIdentifier and nib utility extension
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 { }
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