Skip to content

Commit

Permalink
Added Header/Footer register+dequeue to UICollectionView
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrommcarrasco committed Mar 31, 2019
1 parent dfb6ee1 commit 9d9bee8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sucrose.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.swift_version = "5.0"
s.name = "Sucrose"
s.version = "3.1.1"
s.version = "3.2.0"
s.summary = "🍬 Everyday sugar"
s.description = "Collection of handy methods & objects"

Expand Down
29 changes: 27 additions & 2 deletions Sucrose/Extensions/UICollectionView+Cell.swift
Expand Up @@ -9,16 +9,41 @@
import UIKit

public extension UICollectionView {

enum SupplementaryViewKind {
case header
case footer

var value: String {
switch self {
case .header: return UICollectionView.elementKindSectionHeader
case .footer: return UICollectionView.elementKindSectionFooter
}
}
}

func register<T: UICollectionViewCell>(_: T.Type) {
register(T.self, forCellWithReuseIdentifier: T.name)
}

func register<T: UICollectionReusableView>(_: T.Type, as kind: SupplementaryViewKind) {
register(T.self, forSupplementaryViewOfKind: kind.value, withReuseIdentifier: T.name)
}

func dequeue<T: UICollectionViewCell>(_ type: T.Type, for indexPath: IndexPath) -> T {
guard let cell = self.dequeueReusableCell(withReuseIdentifier: type.name, for: indexPath) as? T else {
func dequeue<T: UICollectionViewCell>(_ type: T.Type, in indexPath: IndexPath) -> T {
guard let cell = dequeueReusableCell(withReuseIdentifier: type.name, for: indexPath) as? T else {
fatalError("Unknown Cell at \(indexPath)")
}

return cell
}

func dequeue<T: UICollectionReusableView>(_ type: T.Type, in indexPath: IndexPath, as kind: SupplementaryViewKind) -> T {

guard let view = dequeueReusableSupplementaryView(ofKind: kind.value, withReuseIdentifier: T.name, for: indexPath) as? T else {
fatalError("Couldn't dequeue Header/Footer named \(T.name)")
}

return view
}
}

0 comments on commit 9d9bee8

Please sign in to comment.