UICollectionView indexPathsForVisibleItems顺序

use*_*120 1 objective-c ios uicollectionview

我使用UICollectionView显示第2列中的内容.我需要跟踪可见单元格中的更改.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray* visibleCellIndex = collectionViewCatalog.indexPathsForVisibleItems;

    NSIndexPath* newFirstVisibleCell = [visibleCellIndex firstObject];
    NSIndexPath* newLastVisibleCell = [visibleCellIndex lastObject];
    NSLog(@"Visible cells [ %i ; %i]",newFirstVisibleCell.row,newLastVisibleCell.row);
}
Run Code Online (Sandbox Code Playgroud)

Log : Visible cells [ 0 ; 3],但有时崩溃顺序Visible cells [ 5 ; 4]Visible cells [ 12 ; 11] Log indexPathsForVisibleItems for cells [5; 4]:

(
    "<NSIndexPath: 0x1653bb90> {length = 2, path = 0 - 5}",
    "<NSIndexPath: 0x1655ab10> {length = 2, path = 0 - 2}",
    "<NSIndexPath: 0x1655aa80> {length = 2, path = 0 - 3}",
    "<NSIndexPath: 0x16539680> {length = 2, path = 0 - 4}"
)
Run Code Online (Sandbox Code Playgroud)

在此之后(可见细胞[5; 4])下一个日志信息:Visible cells [ 6 ; 9]从5到9个细胞没有记录

我只有20个细胞.为什么在数组indexPathsForVisibleItems中崩溃顺序indexPaths?

Tho*_*Haz 12

从NSIndexPath标头:

// comparison support
- (NSComparisonResult)compare:(NSIndexPath *)otherObject; // sorting an array of indexPaths using this comparison results in an array representing nodes in depth-first traversal order
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

NSArray *sortedVisibleItems = [[collectionViewCatalog indexPathsForVisibleItems] sortedArrayUsingSelector:@selector(compare:)];
Run Code Online (Sandbox Code Playgroud)