Skip to content
This repository was archived by the owner on Jun 17, 2023. It is now read-only.

Commit e7f6a32

Browse files
authored
Merge pull request #10 from zzzworm/grant.zhou/layout_multisections
Support multi-section
2 parents 50a185f + d5ab697 commit e7f6a32

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

Lib/CollectionViewPagingLayout.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
4343
}
4444

4545
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, +)
4749
}
4850

4951

@@ -88,8 +90,19 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
8890
let endIndex = min(numberOfItems, initialEndIndex + startIndexOutOfBounds)
8991

9092
var attributesArray: [UICollectionViewLayoutAttributes] = []
93+
var section = 0
94+
var numberOfItemsInSection = collectionView?.numberOfItems(inSection: section) ?? 0
95+
var numberOfItemsInPrevSections = 0
9196
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))
93106
let pageIndex = CGFloat(index)
94107
let progress = pageIndex - currentScrollOffset
95108
var zIndex = Int(-abs(round(progress)))

0 commit comments

Comments
 (0)