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

Commit 5eecece

Browse files
1.6.3 (#143)
Fixes a bug related to cell recycling #142
1 parent 272fe9d commit 5eecece

File tree

9 files changed

+24
-41
lines changed

9 files changed

+24
-41
lines changed

Demo/ASCollectionViewDemo/MainView.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ struct MainView: View
6060
}
6161
Section(header: Text("Modified examples"))
6262
{
63-
NavigationLink(destination: PhotoGridScreen(startingAtBottom: true))
64-
{
65-
Image(systemName: "hammer")
66-
Text("Photo grid (Starting at bottom)")
67-
}
6863
NavigationLink(destination: TagsScreen(shrinkToSize: true))
6964
{
7065
Image(systemName: "hammer")

Demo/ASCollectionViewDemo/Screens/AdjustableLayout/AdjustableGridScreen.swift

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct AdjustableGridScreen: View
2727
id: 0,
2828
data: data,
2929
onCellEvent: onCellEvent)
30-
{ item, state in
30+
{ item, _ in
3131
ZStack(alignment: .bottomTrailing)
3232
{
3333
GeometryReader
@@ -36,24 +36,15 @@ struct AdjustableGridScreen: View
3636
.aspectRatio(1, contentMode: .fill)
3737
.frame(width: geom.size.width, height: geom.size.height)
3838
.clipped()
39-
.opacity(state.isSelected ? 0.7 : 1.0)
4039
}
4140

42-
if state.isSelected
43-
{
44-
ZStack
45-
{
46-
Circle()
47-
.fill(Color.blue)
48-
Circle()
49-
.strokeBorder(Color.white, lineWidth: 2)
50-
Image(systemName: "checkmark")
51-
.font(.system(size: 10, weight: .bold))
52-
.foregroundColor(.white)
53-
}
54-
.frame(width: 20, height: 20)
41+
Text("\(item.offset)")
42+
.font(.headline)
43+
.bold()
44+
.padding(2)
45+
.background(Color(.systemBackground).opacity(0.5))
46+
.cornerRadius(4)
5547
.padding(10)
56-
}
5748
}
5849
}
5950
}

Demo/ASCollectionViewDemo/Screens/PhotoGrid/PhotoGridScreen.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import UIKit
66

77
struct PhotoGridScreen: View
88
{
9-
var startingAtBottom: Bool = false
10-
119
@State var data: [Post] = DataSource.postsForGridSection(1, number: 1000)
1210
@State var selectedItems: Set<Int> = []
1311

@@ -67,7 +65,6 @@ struct PhotoGridScreen: View
6765
ASCollectionView(
6866
section: section)
6967
.layout(self.layout)
70-
.initialScrollPosition(startingAtBottom ? .bottom : nil)
7168
.edgesIgnoringSafeArea(.all)
7269
.navigationBarTitle("Explore", displayMode: .large)
7370
.navigationBarItems(

Sources/ASCollectionView/Cells/ASCollectionViewCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
3131
{
3232
hostingController?.viewController.removeFromParent()
3333
hostingController.map { vc.addChild($0.viewController) }
34+
attachView()
3435
hostingController?.viewController.didMove(toParent: vc)
3536
}
3637
}
@@ -47,7 +48,6 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
4748
detachViews()
4849
return
4950
}
50-
guard !isHidden else { return }
5151
if hcView.superview != contentView
5252
{
5353
contentView.subviews.forEach { $0.removeFromSuperview() }

Sources/ASCollectionView/Cells/ASCollectionViewSupplementaryView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
1818
{
1919
hostingController?.viewController.removeFromParent()
2020
hostingController.map { vc?.addChild($0.viewController) }
21+
attachView()
2122
hostingController?.viewController.didMove(toParent: vc)
2223
}
2324
}
@@ -34,7 +35,6 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
3435
detachViews()
3536
return
3637
}
37-
guard !isHidden else { return }
3838
if hcView.superview != self
3939
{
4040
subviews.forEach { $0.removeFromSuperview() }

Sources/ASCollectionView/Cells/ASTableViewCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
3939
{
4040
hostingController?.viewController.removeFromParent()
4141
hostingController.map { vc.addChild($0.viewController) }
42+
attachView()
4243
hostingController?.viewController.didMove(toParent: vc)
4344
}
4445
}
@@ -55,7 +56,6 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
5556
detachViews()
5657
return
5758
}
58-
guard !isHidden else { return }
5959
if hcView.superview != contentView
6060
{
6161
contentView.subviews.forEach { $0.removeFromSuperview() }

Sources/ASCollectionView/Cells/ASTableViewSupplementaryView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
2929
{
3030
hostingController?.viewController.removeFromParent()
3131
hostingController.map { vc.addChild($0.viewController) }
32+
attachView()
3233
hostingController?.viewController.didMove(toParent: vc)
3334
}
3435
}
@@ -45,7 +46,6 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
4546
detachViews()
4647
return
4748
}
48-
guard !isHidden else { return }
4949
if hcView.superview != contentView
5050
{
5151
contentView.subviews.forEach { $0.removeFromSuperview() }

Sources/ASCollectionView/Implementation/ASCollectionView.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
129129

130130
// MARK: Private tracking variables
131131

132-
private var hasMovedToParent = true
132+
private var hasDoneInitialSetup = true
133133
private var hasSetInitialScrollPosition = false
134134

135135
private var hasFiredBoundaryNotificationForBoundary: Set<Boundary> = []
@@ -277,7 +277,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
277277

278278
func populateDataSource(animated: Bool = true, transaction: Transaction? = nil)
279279
{
280-
guard hasMovedToParent else { return }
280+
guard hasDoneInitialSetup else { return }
281281
collectionViewController.map { registerSupplementaries(forCollectionView: $0.collectionView) } // New sections might involve new types of supplementary...
282282
let snapshot = ASDiffableDataSourceSnapshot(sections:
283283
parent.sections.map {
@@ -293,7 +293,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
293293

294294
func updateContent(_ cv: UICollectionView, transaction: Transaction?)
295295
{
296-
guard hasMovedToParent else { return }
296+
guard hasDoneInitialSetup else { return }
297297

298298
let transactionAnimationEnabled = (transaction?.animation != nil) && !(transaction?.disablesAnimations ?? false)
299299
populateDataSource(
@@ -330,9 +330,9 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
330330

331331
func onMoveToParent()
332332
{
333-
guard !hasMovedToParent else { return }
333+
guard !hasDoneInitialSetup else { return }
334334

335-
hasMovedToParent = true
335+
hasDoneInitialSetup = true
336336
populateDataSource(animated: false)
337337
}
338338

@@ -465,7 +465,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
465465
func updateLayout()
466466
{
467467
guard
468-
hasMovedToParent,
468+
hasDoneInitialSetup,
469469
let collectionViewController = collectionViewController else { return }
470470
// Configure any custom layout
471471
parent.layout.configureLayout(layoutObject: collectionViewController.collectionView.collectionViewLayout)
@@ -474,15 +474,15 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
474474
if parent.shouldRecreateLayoutOnStateChange
475475
{
476476
let newLayout = parent.layout.makeLayout(withCoordinator: self)
477-
collectionViewController.collectionView.setCollectionViewLayout(newLayout, animated: parent.shouldAnimateRecreatedLayoutOnStateChange && hasMovedToParent)
477+
collectionViewController.collectionView.setCollectionViewLayout(newLayout, animated: parent.shouldAnimateRecreatedLayoutOnStateChange && hasDoneInitialSetup)
478478
}
479479
// If enabled, invalidate the layout
480480
else if parent.shouldInvalidateLayoutOnStateChange
481481
{
482482
let changes = {
483483
collectionViewController.collectionViewLayout.invalidateLayout()
484484
}
485-
if parent.shouldAnimateInvalidatedLayoutOnStateChange, hasMovedToParent
485+
if parent.shouldAnimateInvalidatedLayoutOnStateChange, hasDoneInitialSetup
486486
{
487487
UIView.animate(
488488
withDuration: 0.4,

Sources/ASCollectionView/Implementation/ASTableView.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
106106

107107
// MARK: Private tracking variables
108108

109-
private var hasMovedToParent = false
109+
private var hasDoneInitialSetup = false
110110

111111
// MARK: Caching
112112

@@ -211,7 +211,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
211211

212212
func populateDataSource(animated: Bool = true, transaction: Transaction? = nil)
213213
{
214-
guard hasMovedToParent else { return }
214+
guard hasDoneInitialSetup else { return }
215215
let snapshot = ASDiffableDataSourceSnapshot(sections:
216216
parent.sections.map {
217217
ASDiffableDataSourceSnapshot.Section(id: $0.id, elements: $0.itemIDs)
@@ -226,7 +226,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
226226

227227
func updateContent(_ tv: UITableView, transaction: Transaction?)
228228
{
229-
guard hasMovedToParent else { return }
229+
guard hasDoneInitialSetup else { return }
230230

231231
let transactionAnimationEnabled = (transaction?.animation != nil) && !(transaction?.disablesAnimations ?? false)
232232
populateDataSource(animated: parent.animateOnDataRefresh && transactionAnimationEnabled, transaction: transaction)
@@ -273,9 +273,9 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
273273

274274
func onMoveToParent()
275275
{
276-
guard !hasMovedToParent else { return }
276+
guard !hasDoneInitialSetup else { return }
277277

278-
hasMovedToParent = true
278+
hasDoneInitialSetup = true
279279
populateDataSource(animated: false)
280280
tableViewController.map { checkIfReachedBottom($0.tableView) }
281281
}

0 commit comments

Comments
 (0)