项目中遇到的问题,当自定义键盘上的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];