Skip to content

Commit

Permalink
Merge pull request #27 from MohammadRezaAnsari/addSomeExtensions
Browse files Browse the repository at this point in the history
Add some for UIView and UITableView and UICollectionView
  • Loading branch information
MahshidSharif authored Feb 27, 2022
2 parents 0572e00 + c5b35d5 commit 38cbb6f
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// UICollectionView+Dequeue.swift
//
//
// Created by Mahshid Sharif on 2/26/22.
//

import UIKit

public extension UICollectionView {

final func customDequeueReusableCell<T: UICollectionViewCell>(
for indexPath: IndexPath,
cellType: T.Type = T.self) -> T {

guard let cell = self.dequeueReusableCell(
withReuseIdentifier: cellType.reuseIdentifier,
for: indexPath) as? T else {
fatalError(
"Failed to dequeue a cell with identifier \(cellType.reuseIdentifier) matching type \(cellType.self). "
)
}
return cell
}

final func customDequeueReusableHeaderFooterView<T: UICollectionReusableView>(
kind: SupplementaryViewKind,
for indexPath: IndexPath,
_ viewType: T.Type = T.self) -> T? {

guard let view = self.dequeueReusableSupplementaryView(
ofKind: kind.rawValue,
withReuseIdentifier: viewType.reuseIdentifier,
for: indexPath) as? T? else {
fatalError(
"Failed to dequeue a header/footer with identifier \(viewType.reuseIdentifier) "
+ "matching type \(viewType.self). "
)
}
return view
}
}

public enum SupplementaryViewKind {
case header
case footer

var rawValue: String {
switch self {
case .header: return UICollectionView.elementKindSectionHeader
case .footer: return UICollectionView.elementKindSectionFooter
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// UICollectionView+Register.swift
//
//
// Created by Mahshid Sharif on 2/26/22.
//

import UIKit

public extension UICollectionView {

final func customRegister<T: UICollectionViewCell>(cellType: T.Type) {
self.register(cellType.self,
forCellWithReuseIdentifier: cellType.reuseIdentifier)
}

final func customRegister<T: UICollectionReusableView>(headerFooterViewType: T.Type) {
self.register(headerFooterViewType.self,
forCellWithReuseIdentifier: headerFooterViewType.reuseIdentifier)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ReusableCellProtocol.swift
//
//
// Created by MohammadReza Ansary on 2/19/22.
//

import UIKit

protocol ReusableCellProtocol: AnyObject {
static var reuseIdentifier: String { get }
}

extension ReusableCellProtocol {
static var reuseIdentifier: String {
return String(describing: self)
}
}

extension UITableViewCell: ReusableCellProtocol {}
extension UITableViewHeaderFooterView: ReusableCellProtocol {}

extension UICollectionViewCell: ReusableCellProtocol {}
extension UICollectionReusableView: ReusableCellProtocol {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// UITableView+Dequeue.swift
//
//
// Created by Mahshid Sharif on 2/26/22.
//

import UIKit

public extension UITableView {

final func customDequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath, cellType: T.Type = T.self) -> T {
guard let cell = self.dequeueReusableCell(withIdentifier: cellType.reuseIdentifier, for: indexPath) as? T else {
fatalError(
"Failed to dequeue a cell with identifier \(cellType.reuseIdentifier) matching type \(cellType.self). "
)
}
return cell
}

final func customDequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(_ viewType: T.Type = T.self) -> T? {
guard let view = self.dequeueReusableHeaderFooterView(withIdentifier: viewType.reuseIdentifier) as? T? else {
fatalError(
"Failed to dequeue a header/footer with identifier \(viewType.reuseIdentifier) "
+ "matching type \(viewType.self). "
)
}
return view
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// UITableView+Register.swift
//
//
// Created by MohammadReza Ansary on 2/19/22.
//

import UIKit

public extension UITableView {

final func customRegister<T: UITableViewCell>(cellType: T.Type) {
self.register(cellType.self,
forCellReuseIdentifier: cellType.reuseIdentifier)
}

final func customRegister<T: UITableViewHeaderFooterView>(headerFooterViewType: T.Type) {
self.register(headerFooterViewType.self,
forHeaderFooterViewReuseIdentifier: headerFooterViewType.reuseIdentifier)
}
}
21 changes: 21 additions & 0 deletions Sources/ExtensionKit/UIExtensions/UIView/UIView+SetHeight.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// UIView+SetHeight.swift
//
//
// Created by Mahshid Sharif on 2/26/22.
//

import UIKit

extension UIView {

public func setHeight(_ height: CGFloat, animateTime: TimeInterval = 0) {

guard let heightConstant = self.constraints.first(
where: { $0.firstAttribute == .height && $0.relation == .equal }) else { return }
heightConstant.constant = CGFloat(height)

UIView.animate(withDuration: animateTime,
animations: { self.superview?.layoutIfNeeded() })
}
}

0 comments on commit 38cbb6f

Please sign in to comment.