@@ -631,31 +631,39 @@ final class StateController {
631
631
break
632
632
}
633
633
634
- guard let firstMatchIndex = Array ( section. items. enumerated ( ) ) . binarySearch ( predicate: { itemIndex, _ in
635
- let itemPath = ItemPath ( item: itemIndex, section: sectionIndex)
636
- guard let itemFrame = self . itemFrame ( for: itemPath, kind: . cell, at: state, isFinal: true ) else {
637
- return . orderedDescending
638
- }
639
- if itemFrame. intersects ( visibleRect) {
640
- return . orderedSame
641
- }
642
- if itemFrame. minY > visibleRect. maxY {
643
- return . orderedDescending
644
- }
645
- return . orderedAscending
646
- } ) else {
647
- break
648
- }
649
- var startingIndex = firstMatchIndex
650
- for itemIndex in ( 0 ..< firstMatchIndex) . reversed ( ) {
651
- let itemPath = ItemPath ( item: itemIndex, section: sectionIndex)
652
- guard let itemFrame = itemFrame ( for: itemPath, kind: . cell, at: state, isFinal: true ) else {
653
- continue
654
- }
655
- guard itemFrame. maxY >= visibleRect. minY else {
656
- break
634
+ var startingIndex = 0
635
+ // If header is not visible
636
+ if traverseState == . notFound {
637
+ // Find if any of the items of the section is visible
638
+ if let firstMatchIndex = Array ( section. items. enumerated ( ) ) . binarySearch ( predicate: { itemIndex, _ in
639
+ let itemPath = ItemPath ( item: itemIndex, section: sectionIndex)
640
+ guard let itemFrame = self . itemFrame ( for: itemPath, kind: . cell, at: state, isFinal: true ) else {
641
+ return . orderedDescending
642
+ }
643
+ if itemFrame. intersects ( visibleRect) {
644
+ return . orderedSame
645
+ }
646
+ if itemFrame. minY > visibleRect. maxY {
647
+ return . orderedDescending
648
+ }
649
+ return . orderedAscending
650
+ } ) {
651
+ // Find first item that is visible
652
+ startingIndex = firstMatchIndex
653
+ for itemIndex in ( 0 ..< firstMatchIndex) . reversed ( ) {
654
+ let itemPath = ItemPath ( item: itemIndex, section: sectionIndex)
655
+ guard let itemFrame = itemFrame ( for: itemPath, kind: . cell, at: state, isFinal: true ) else {
656
+ continue
657
+ }
658
+ guard itemFrame. maxY >= visibleRect. minY else {
659
+ break
660
+ }
661
+ startingIndex = itemIndex
662
+ }
663
+ } else {
664
+ // Otherwise we can safely skip all the items in the section and go to footer.
665
+ startingIndex = section. items. count - 1
657
666
}
658
- startingIndex = itemIndex
659
667
}
660
668
661
669
if startingIndex < section. items. count {
0 commit comments