Skip to content

Commit b2a6dbe

Browse files
committed
fixes on collection views
1 parent c3dcfc5 commit b2a6dbe

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

Presentables/Classes/Collection views/PresentableCollectionViewDataManager.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010

1111
open class PresentableCollectionViewDataManager: NSObject, PresentableManager, UICollectionViewDataSource, UICollectionViewDelegate {
1212

13+
// TODO: Can we remove this?
1314
public var needsReloadData: (()->())?
1415
public var sizeForHeaderInSection: ((_ section: Int)->(CGSize))?
1516
public var sizeForFooterInSection: ((_ section: Int)->(CGSize))?
@@ -33,6 +34,7 @@ open class PresentableCollectionViewDataManager: NSObject, PresentableManager, U
3334
// MARK: Actions
3435

3536
public func reloadData() {
37+
collectionView?.collectionViewLayout.invalidateLayout()
3638
collectionView?.reloadData()
3739
}
3840

@@ -68,38 +70,44 @@ open class PresentableCollectionViewDataManager: NSObject, PresentableManager, U
6870
fatalError()
6971
}
7072
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: presentable.identifier, for: indexPath)
73+
presentable.runConfigure(with: view)
7174
return view
7275
}
7376
else {
7477
guard let presentable: AnyPresentable = data.footer(forSection: indexPath.section) else {
7578
fatalError()
7679
}
7780
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: presentable.identifier, for: indexPath)
81+
presentable.runConfigure(with: view)
7882
return view
7983
}
8084
}
8185

86+
// MARK: Delegate
87+
88+
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
89+
let presentable: AnyPresentable = data.presentable(forIndexPath: indexPath)
90+
presentable.selected?()
91+
selectedItem?((presentable: presentable, indexPath: indexPath, collectionView: collectionView))
92+
}
93+
94+
}
95+
96+
97+
extension PresentableCollectionViewDataManager: UICollectionViewDelegateFlowLayout {
98+
8299
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
83100
guard let _ = data.header(forSection: section) else {
84101
return CGSize.zero
85102
}
86103
return sizeForHeaderInSection?(section) ?? CGSize(width: collectionView.frame.width, height: 44)
87104
}
88-
105+
89106
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
90107
guard let _ = data.footer(forSection: section) else {
91108
return CGSize.zero
92109
}
93110
return sizeForFooterInSection?(section) ?? CGSize(width: collectionView.frame.width, height: 44)
94111
}
95-
96-
97-
// MARK: Delegate
98-
99-
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
100-
let presentable: AnyPresentable = data.presentable(forIndexPath: indexPath)
101-
presentable.selected?()
102-
selectedItem?((presentable: presentable, indexPath: indexPath, collectionView: collectionView))
103-
}
104-
112+
105113
}

Presentables/Classes/Collection views/Presentables+UICollectionView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extension UICollectionView: PresentableCollectionElement {
2121

2222
func safeReloadData() {
2323
DispatchQueue.main.async {
24+
self.collectionViewLayout.invalidateLayout()
2425
self.reloadData()
2526
}
2627
}

Presentables/Classes/Collection views/UICollectionView+Presentables.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ extension UICollectionView {
1616
register(cellClass, forCellWithReuseIdentifier: identifier)
1717
}
1818

19+
open func register<T>(header viewClass: T.Type) where T: UICollectionReusableView {
20+
let identifier = String(describing: T.self)
21+
register(viewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: identifier)
22+
}
23+
24+
open func register<T>(footer viewClass: T.Type) where T: UICollectionReusableView {
25+
let identifier = String(describing: T.self)
26+
register(viewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: identifier)
27+
}
28+
1929
}

0 commit comments

Comments
 (0)