Skip to content

Instantly share code, notes, and snippets.

@iranjith4
Created November 23, 2018 09:13
Show Gist options
  • Save iranjith4/ad46abdd8d534865698da2813fcde096 to your computer and use it in GitHub Desktop.
Save iranjith4/ad46abdd8d534865698da2813fcde096 to your computer and use it in GitHub Desktop.
reusableview-dequeue-declaration
//Dequeue methods for UICollectionView
func dequeueReusableCell<T: UICollectionViewCell>(for indexPath: IndexPath) -> T where T: ReusableView {
guard let cell = dequeueReusableCell(withReuseIdentifier: T.defaultReuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier: \(T.defaultReuseIdentifier)")
}
return cell
}
func dequeueReusableSupplementaryView<T: UICollectionReusableView>(ofKind: String, indexPath: IndexPath) -> T where T: ReusableView {
guard let supplementaryView = dequeueReusableSupplementaryView(ofKind: ofKind, withReuseIdentifier: T.defaultReuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue supplementary view with identifier: \(T.defaultReuseIdentifier)")
}
return supplementaryView
}
//Dequeue methods for UITableView
func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T where T: ReusableView {
guard let cell = dequeueReusableCell(withIdentifier: T.defaultReuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier: \(T.defaultReuseIdentifier)")
}
return cell
}
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(_ : T.Type) -> T where T: ReusableView {
guard let headerFooter = dequeueReusableHeaderFooterView(withIdentifier: T.defaultReuseIdentifier) as? T else {
fatalError("Could not dequeue Header/Footer with identifier: \(T.defaultReuseIdentifier)")
}
return headerFooter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment