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

Commit da21804

Browse files
author
grant
committed
support multi section collectionview
1 parent 50a185f commit da21804

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

Lib/CollectionViewPagingLayout.swift

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

4545
private var numberOfItems: Int {
46-
collectionView?.numberOfItems(inSection: 0) ?? 0
46+
var count = 0;
47+
if let sectionCount = collectionView?.numberOfSections {
48+
for i in (0 ..< sectionCount) {
49+
count += collectionView?.numberOfItems(inSection: i) ?? 0
50+
}
51+
}
52+
return count
4753
}
4854

4955

@@ -88,8 +94,23 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
8894
let endIndex = min(numberOfItems, initialEndIndex + startIndexOutOfBounds)
8995

9096
var attributesArray: [UICollectionViewLayoutAttributes] = []
97+
let sectionCount = collectionView?.numberOfSections ?? 0
9198
for index in startIndex..<endIndex {
92-
let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(row: index, section: 0))
99+
var section = 0
100+
var row = 0
101+
var accIndex = 0
102+
for i in (0 ..< sectionCount) {
103+
if index - accIndex < collectionView!.numberOfItems(inSection: i) {
104+
section = i
105+
row = index - accIndex
106+
break;
107+
}
108+
else{
109+
accIndex += collectionView!.numberOfItems(inSection: i)
110+
}
111+
}
112+
113+
let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(row: row, section: section))
93114
let pageIndex = CGFloat(index)
94115
let progress = pageIndex - currentScrollOffset
95116
var zIndex = Int(-abs(round(progress)))

0 commit comments

Comments
 (0)