UICollectionView的indexPathsForVi

2018-04-27  本文已影响0人  _乱

项目中遇到的问题,当自定义键盘上的UICollectionView无限快速滑动时,使用[[self.collectionView indexPathsForVisibleItems] firstObject]去下标时返回的数据不正确,提供两种方式修改。

方式一:

NSArray* visibleCellIndex = self.collectionView.indexPathsForVisibleItems;

 NSArray *sortedIndexPaths = [visibleCellIndex sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

        NSIndexPath *path1 = (NSIndexPath *)obj1;

        NSIndexPath *path2 = (NSIndexPath *)obj2;

        return [path1 compare:path2];

    }];

    NSIndexPath* indexPath = [sortedIndexPaths firstObject];

方式二:(这个方法很好)

NSArray* visibleCellIndex = self.collectionView.indexPathsForVisibleItems;

   NSArray *sortedIndexPaths = [[self.collectionView indexPathsForVisibleItems] sortedArrayUsingSelector:@selector(compare:)];

NSIndexPath* indexPath = [sortedIndexPaths firstObject];

上一篇 下一篇

猜你喜欢

热点阅读