Skip to content

Commit 3775f11

Browse files
committed
Fixed header process for collection view.
1 parent a000880 commit 3775f11

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Sources/SPDiffable/DataSource/SPDiffableCollectionDataSource.swift

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,34 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
8585

8686
// Update current sections.
8787

88+
// If you don't use composition layout, no way say to collection system
89+
// about header elements. Its using random, sometimes its
90+
// cell provider call, sometimes it call supplementary.
91+
// In this case we shoudn't set header to section snapshot.
92+
// For this case it condition only.
93+
let headerAsFirstElement: Bool = {
94+
if collectionView?.collectionViewLayout is UICollectionViewFlowLayout {
95+
return false
96+
}
97+
if collectionView?.collectionViewLayout is UICollectionViewCompositionalLayout {
98+
return true
99+
}
100+
return true
101+
}()
102+
88103
for section in sections {
89104
var sectionSnapshot = SPDiffableSectionSnapshot()
90-
let header = section.header
91-
if let header = header {
92-
sectionSnapshot.append([header])
105+
106+
if headerAsFirstElement {
107+
let header = section.header
108+
if let header = header {
109+
sectionSnapshot.append([header])
110+
}
111+
sectionSnapshot.append(section.items, to: header)
112+
} else {
113+
sectionSnapshot.append(section.items)
93114
}
94-
sectionSnapshot.append(section.items, to: header)
115+
95116
sectionSnapshot.expand(sectionSnapshot.items)
96117
apply(sectionSnapshot, to: section, animatingDifferences: animated)
97118
}

0 commit comments

Comments
 (0)