-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from MohammadRezaAnsari/addSomeExtensions
Add some for UIView and UITableView and UICollectionView
- Loading branch information
Showing
6 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Sources/ExtensionKit/UIExtensions/UICollectionView/UICollectionView+Dequeue.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Sources/ExtensionKit/UIExtensions/UICollectionView/UICollectionView+Register.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/ExtensionKit/UIExtensions/UITableView/ReusableCellProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
30 changes: 30 additions & 0 deletions
30
Sources/ExtensionKit/UIExtensions/UITableView/UITableView+Dequeue.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Sources/ExtensionKit/UIExtensions/UITableView/UITableView+Register.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
Sources/ExtensionKit/UIExtensions/UIView/UIView+SetHeight.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() }) | ||
} | ||
} |