@@ -43,7 +43,9 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
43
43
}
44
44
45
45
private var numberOfItems : Int {
46
- collectionView? . numberOfItems ( inSection: 0 ) ?? 0
46
+ return ( 0 ..< ( collectionView? . numberOfSections ?? 0 ) )
47
+ . compactMap { collectionView? . numberOfItems ( inSection: $0) }
48
+ . reduce ( 0 , + )
47
49
}
48
50
49
51
@@ -88,8 +90,19 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
88
90
let endIndex = min ( numberOfItems, initialEndIndex + startIndexOutOfBounds)
89
91
90
92
var attributesArray : [ UICollectionViewLayoutAttributes ] = [ ]
93
+ var section = 0
94
+ var numberOfItemsInSection = collectionView? . numberOfItems ( inSection: section) ?? 0
95
+ var numberOfItemsInPrevSections = 0
91
96
for index in startIndex..< endIndex {
92
- let cellAttributes = UICollectionViewLayoutAttributes ( forCellWith: IndexPath ( row: index, section: 0 ) )
97
+ var item = index - numberOfItemsInPrevSections
98
+ while item >= numberOfItemsInSection {
99
+ numberOfItemsInPrevSections += numberOfItemsInSection
100
+ section += 1
101
+ numberOfItemsInSection = collectionView? . numberOfItems ( inSection: section) ?? 0
102
+ item = index - numberOfItemsInPrevSections
103
+ }
104
+
105
+ let cellAttributes = UICollectionViewLayoutAttributes ( forCellWith: IndexPath ( item: item, section: section) )
93
106
let pageIndex = CGFloat ( index)
94
107
let progress = pageIndex - currentScrollOffset
95
108
var zIndex = Int ( - abs( round ( progress) ) )
0 commit comments