Created
June 25, 2018 23:56
-
-
Save aybekckaya/51f9404ed0582ea4656c715c83a00294 to your computer and use it in GitHub Desktop.
easy view loading with {UIView}.identifier
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
protocol ComponentIdentifier: class { | |
static var identifier: String { get } | |
} | |
extension ComponentIdentifier { | |
static var identifier: String { return String(describing: self) } | |
} | |
extension UIView { | |
class func loadNib<G: ComponentIdentifier>() -> G { | |
guard let view = Bundle.main.loadNibNamed(G.identifier, owner: nil, options: nil)?[0] as? G else { fatalError() } | |
return view | |
} | |
} | |
extension UITableView { | |
func deque<G: ComponentIdentifier>(indexPath: IndexPath) -> G { | |
guard let cell = self.dequeueReusableCell(withIdentifier: G.identifier, for: indexPath) as? G else { fatalError() } | |
return cell | |
} | |
} | |
extension UICollectionView { | |
func deque<G: ComponentIdentifier>(indexPath: IndexPath) -> G { | |
guard let cell = self.dequeueReusableCell(withReuseIdentifier: G.identifier, for: indexPath) as? G else { fatalError() } | |
return cell | |
} | |
func dequeHeaderView<G: ComponentIdentifier>(indexPath:IndexPath)->G { | |
guard let view = self.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: G.identifier, for: indexPath) as? G else { fatalError() } | |
return view | |
} | |
} | |
protocol ControllerNibProtocol { | |
static var identifier:String { get } | |
} | |
extension ControllerNibProtocol { | |
static var identifier: String { return String(describing: self) } | |
} | |
extension UIView: ComponentIdentifier {} | |
extension UIViewController:ControllerNibProtocol {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment