@@ -633,11 +633,10 @@ final class StateController {
633
633
634
634
var startingIndex = 0
635
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
636
+ if traverseState == . notFound, !section. items. isEmpty {
637
+ func predicate( itemIndex: Int ) -> ComparisonResult {
639
638
let itemPath = ItemPath ( item: itemIndex, section: sectionIndex)
640
- guard let itemFrame = self . itemFrame ( for: itemPath, kind: . cell, at: state, isFinal: true ) else {
639
+ guard let itemFrame = itemFrame ( for: itemPath, kind: . cell, at: state, isFinal: true ) else {
641
640
return . orderedDescending
642
641
}
643
642
if itemFrame. intersects ( visibleRect) {
@@ -647,7 +646,11 @@ final class StateController {
647
646
return . orderedDescending
648
647
}
649
648
return . orderedAscending
650
- } ) {
649
+ }
650
+
651
+ // Find if any of the items of the section is visible
652
+ if [ ComparisonResult . orderedSame, . orderedDescending] . contains ( predicate ( itemIndex: section. items. count - 1 ) ) ,
653
+ let firstMatchIndex = Array ( 0 ... section. items. count - 1 ) . binarySearch ( predicate: predicate) {
651
654
// Find first item that is visible
652
655
startingIndex = firstMatchIndex
653
656
for itemIndex in ( 0 ..< firstMatchIndex) . reversed ( ) {
@@ -662,7 +665,7 @@ final class StateController {
662
665
}
663
666
} else {
664
667
// Otherwise we can safely skip all the items in the section and go to footer.
665
- startingIndex = section. items. count - 1
668
+ startingIndex = section. items. count
666
669
}
667
670
}
668
671
0 commit comments