From ef3e2c02974e49724cfa6c5347ee070edbf7f97f Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Tue, 3 Oct 2017 22:06:50 -0500 Subject: [PATCH] Add generic method for header/footer dequeueing and change extension to MessagesCollectionView --- Sources/UICollectionView+Extensions.swift | 52 +++++++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/Sources/UICollectionView+Extensions.swift b/Sources/UICollectionView+Extensions.swift index 3d2b7c3d..dc5d1ff1 100644 --- a/Sources/UICollectionView+Extensions.swift +++ b/Sources/UICollectionView+Extensions.swift @@ -1,19 +1,35 @@ -// -// UICollectionView+Extensions.swift -// MessageKit -// -// Created by Frederic Barthelemy on 10/3/17. -// Copyright © 2017 MessageKit. All rights reserved. -// +/* + MIT License + + Copyright (c) 2017 MessageKit + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ import Foundation /// Optional Cell Protocol to Simplify registration/cell type loading in a generic way -protocol CollectionViewReusable: class { +public protocol CollectionViewReusable: class { static func reuseIdentifier() -> String } -extension UICollectionView { +public extension MessagesCollectionView { /// Registers a particular cell using its reuse-identifier func register(_ cellClass: CellType.Type) { register(cellClass, forCellWithReuseIdentifier: CellType.reuseIdentifier()) @@ -33,4 +49,22 @@ extension UICollectionView { } return cell } + + /// Generically dequeues a header of the correct type allowing you to avoid scattering your code with guard-let-else-fatal + func dequeueReusableHeaderView(_ viewClass: ViewType.Type, for indexPath: IndexPath) -> ViewType { + let view = dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: viewClass.reuseIdentifier(), for: indexPath) + guard let viewType = view as? ViewType else { + fatalError("Unable to dequeue \(String(describing: viewClass)) with reuseId of \(viewClass.reuseIdentifier())") + } + return viewType + } + + /// Generically dequeues a footer of the correct type allowing you to avoid scattering your code with guard-let-else-fatal + func dequeueReusableFooterView(_ viewClass: ViewType.Type, for indexPath: IndexPath) -> ViewType { + let view = dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: viewClass.reuseIdentifier(), for: indexPath) + guard let viewType = view as? ViewType else { + fatalError("Unable to dequeue \(String(describing: viewClass)) with reuseId of \(viewClass.reuseIdentifier())") + } + return viewType + } }