Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Created June 25, 2018 23:56
Show Gist options
  • Save aybekckaya/51f9404ed0582ea4656c715c83a00294 to your computer and use it in GitHub Desktop.
Save aybekckaya/51f9404ed0582ea4656c715c83a00294 to your computer and use it in GitHub Desktop.
easy view loading with {UIView}.identifier
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