Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 272fe9d

Browse files
V1.6.2 (#141)
* Attach views in layoutSubviews (as apple does in some of their UI), fixes #130
1 parent 44e46a9 commit 272fe9d

File tree

8 files changed

+40
-55
lines changed

8 files changed

+40
-55
lines changed

ASCollectionView-SwiftUI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = 'ASCollectionView-SwiftUI'
4-
s.version = '1.6.0'
4+
s.version = '1.6.2'
55
s.summary = 'A SwiftUI collection view with support for custom layouts, preloading, and more. '
66

77
s.description = <<-DESC

Sources/ASCollectionView/Cells/ASCollectionViewCell.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,8 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
3131
{
3232
hostingController?.viewController.removeFromParent()
3333
hostingController.map { vc.addChild($0.viewController) }
34-
attachView()
3534
hostingController?.viewController.didMove(toParent: vc)
3635
}
37-
else
38-
{
39-
attachView()
40-
}
4136
}
4237

4338
func didDisappear()
@@ -49,9 +44,10 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
4944
{
5045
guard let hcView = hostingController?.viewController.view else
5146
{
52-
contentView.subviews.forEach { $0.removeFromSuperview() }
47+
detachViews()
5348
return
5449
}
50+
guard !isHidden else { return }
5551
if hcView.superview != contentView
5652
{
5753
contentView.subviews.forEach { $0.removeFromSuperview() }
@@ -60,6 +56,11 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
6056
}
6157
}
6258

59+
private func detachViews()
60+
{
61+
contentView.subviews.forEach { $0.removeFromSuperview() }
62+
}
63+
6364
var shouldSkipNextRefresh: Bool = true
6465

6566
override func prepareForReuse()
@@ -75,6 +76,8 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
7576
{
7677
super.layoutSubviews()
7778

79+
attachView()
80+
7881
if hostingController?.viewController.view.frame != contentView.bounds
7982
{
8083
UIView.performWithoutAnimation {

Sources/ASCollectionView/Cells/ASCollectionViewSupplementaryView.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,14 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
1212
var selfSizingConfig: ASSelfSizingConfig = .init(selfSizeHorizontally: true, selfSizeVertically: true)
1313
var maxSizeForSelfSizing: ASOptionalSize = .none
1414

15-
func setupForEmpty()
16-
{
17-
hostingController = nil
18-
attachView()
19-
}
20-
2115
func willAppear(in vc: UIViewController?)
2216
{
2317
if hostingController?.viewController.parent != vc
2418
{
2519
hostingController?.viewController.removeFromParent()
2620
hostingController.map { vc?.addChild($0.viewController) }
27-
attachView()
2821
hostingController?.viewController.didMove(toParent: vc)
2922
}
30-
else
31-
{
32-
attachView()
33-
}
3423
}
3524

3625
func didDisappear()
@@ -42,9 +31,10 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
4231
{
4332
guard let hcView = hostingController?.viewController.view else
4433
{
45-
subviews.forEach { $0.removeFromSuperview() }
34+
detachViews()
4635
return
4736
}
37+
guard !isHidden else { return }
4838
if hcView.superview != self
4939
{
5040
subviews.forEach { $0.removeFromSuperview() }
@@ -53,6 +43,11 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
5343
}
5444
}
5545

46+
private func detachViews()
47+
{
48+
subviews.forEach { $0.removeFromSuperview() }
49+
}
50+
5651
var shouldSkipNextRefresh: Bool = true
5752
override func prepareForReuse()
5853
{
@@ -64,6 +59,8 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
6459
{
6560
super.layoutSubviews()
6661

62+
attachView()
63+
6764
if hostingController?.viewController.view.frame != bounds
6865
{
6966
UIView.performWithoutAnimation {

Sources/ASCollectionView/Cells/ASTableViewCell.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
1515
{
1616
hostingController?.invalidateCellLayoutCallback = invalidateLayoutCallback
1717
hostingController?.tableViewScrollToCellCallback = scrollToCellCallback
18-
if hostingController !== oldValue, hostingController != nil
19-
{
20-
attachView()
21-
}
2218
}
2319
}
2420

@@ -52,19 +48,14 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
5248
hostingController?.viewController.removeFromParent()
5349
}
5450

55-
override func didMoveToSuperview()
56-
{
57-
attachView()
58-
}
59-
6051
private func attachView()
6152
{
62-
guard superview != nil else { return }
6353
guard let hcView = hostingController?.viewController.view else
6454
{
65-
contentView.subviews.forEach { $0.removeFromSuperview() }
55+
detachViews()
6656
return
6757
}
58+
guard !isHidden else { return }
6859
if hcView.superview != contentView
6960
{
7061
contentView.subviews.forEach { $0.removeFromSuperview() }
@@ -73,6 +64,11 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
7364
}
7465
}
7566

67+
private func detachViews()
68+
{
69+
contentView.subviews.forEach { $0.removeFromSuperview() }
70+
}
71+
7672
var shouldSkipNextRefresh: Bool = true // This is used to avoid double-up in reloaded cells and our update from swiftUI
7773
override func prepareForReuse()
7874
{
@@ -94,6 +90,8 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
9490
{
9591
super.layoutSubviews()
9692

93+
attachView()
94+
9795
if hostingController?.viewController.view.frame != contentView.bounds
9896
{
9997
UIView.performWithoutAnimation {

Sources/ASCollectionView/Cells/ASTableViewSupplementaryView.swift

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ import UIKit
88
class ASTableViewSupplementaryView: UITableViewHeaderFooterView
99
{
1010
var hostingController: ASHostingControllerProtocol?
11-
{
12-
didSet
13-
{
14-
if hostingController !== oldValue, hostingController != nil
15-
{
16-
attachView()
17-
}
18-
}
19-
}
2011

2112
var sectionIDHash: Int?
2213
var supplementaryKind: String?
@@ -32,12 +23,6 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
3223
fatalError("init(coder:) has not been implemented")
3324
}
3425

35-
func setupForEmpty()
36-
{
37-
hostingController = nil
38-
attachView()
39-
}
40-
4126
func willAppear(in vc: UIViewController)
4227
{
4328
if hostingController?.viewController.parent != vc
@@ -53,19 +38,14 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
5338
hostingController?.viewController.removeFromParent()
5439
}
5540

56-
override func didMoveToSuperview()
57-
{
58-
attachView()
59-
}
60-
6141
private func attachView()
6242
{
63-
guard superview != nil else { return }
6443
guard let hcView = hostingController?.viewController.view else
6544
{
66-
contentView.subviews.forEach { $0.removeFromSuperview() }
45+
detachViews()
6746
return
6847
}
48+
guard !isHidden else { return }
6949
if hcView.superview != contentView
7050
{
7151
contentView.subviews.forEach { $0.removeFromSuperview() }
@@ -74,6 +54,11 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
7454
}
7555
}
7656

57+
private func detachViews()
58+
{
59+
contentView.subviews.forEach { $0.removeFromSuperview() }
60+
}
61+
7762
var shouldSkipNextRefresh: Bool = true
7863
override func prepareForReuse()
7964
{
@@ -86,6 +71,8 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
8671
{
8772
super.layoutSubviews()
8873

74+
attachView()
75+
8976
if hostingController?.viewController.view.frame != contentView.bounds
9077
{
9178
UIView.performWithoutAnimation {

Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class ASDiffableDataSourceCollectionView<SectionID: Hashable>: ASDiffableDataSou
7474
guard let cell = supplementaryViewProvider?(collectionView, kind, indexPath) else
7575
{
7676
let empty = collectionView.dequeueReusableSupplementaryView(ofKind: supplementaryEmptyKind, withReuseIdentifier: supplementaryEmptyReuseID, for: indexPath)
77-
(empty as? ASCollectionViewSupplementaryView)?.setupForEmpty()
77+
(empty as? ASCollectionViewSupplementaryView)?.hostingController = nil
7878
return empty
7979
}
8080
return cell

Sources/ASCollectionView/Implementation/ASCollectionView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
252252
else { return nil }
253253

254254
let ifEmpty = {
255-
reusableView.setupForEmpty()
255+
reusableView.hostingController = nil
256256
}
257257

258258
guard let section = self.parent.sections[safe: indexPath.section] else { ifEmpty(); return reusableView }

Sources/ASCollectionView/Implementation/ASTableView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
601601
else { return }
602602

603603
let ifEmpty = {
604-
reusableView.setupForEmpty()
604+
reusableView.hostingController = nil
605605
}
606606

607607
guard let section = parent.sections[safe: sectionIndex] else { ifEmpty(); return }

0 commit comments

Comments
 (0)