Add generic method for header/footer dequeueing and change extension to MessagesCollectionView

This commit is contained in:
Steven Deutsch
2017-10-03 22:06:50 -05:00
parent 0a8df7b7a9
commit ef3e2c0297
+43 -9
View File
@@ -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<CellType: UICollectionViewCell & CollectionViewReusable>(_ 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<ViewType: UICollectionReusableView & CollectionViewReusable>(_ 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<ViewType: UICollectionReusableView & CollectionViewReusable>(_ 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
}
}