当UICollectionView最初加载其数据时,我们如何才能看到Cell IndexPath

Avi*_*Das 2 objective-c uiscrollview nsindexpath ios uicollectionview

有什么方法可以在UICollectionView最初加载其数据时获取indexPathsForVisibleItems.基本上我想要屏幕上当前可见的所有单元格索引路径.我使用下面的代码在scrollViewDidScroll中得到它.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self LoadVisibleItems];

  }

-(void)LoadVisibleItems
{
   NSArray* visibleCellIndex = self.myCollectionView.indexPathsForVisibleItems;

  NSArray *sortedIndexPaths = [visibleCellIndex   sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSIndexPath *path1 = (NSIndexPath *)obj1;
    NSIndexPath *path2 = (NSIndexPath *)obj2;
    return [path1 compare:path2];
}];



NSIndexPath* newFirstVisibleCell = [sortedIndexPaths firstObject];
NSIndexPath* newLastVisibleCell = [sortedIndexPaths lastObject];


}
Run Code Online (Sandbox Code Playgroud)

dop*_*pcn 6

如果我误解了你的问题,请纠正我

如果从故事板加载collectionView

- (void) viewDidLoad {
    [super viewDidLoad];
    NSArray* visibleCellIndex = self.myCollectionView.indexPathsForVisibleItems;
}
Run Code Online (Sandbox Code Playgroud)

如果在一些视图addSubviewcollectionView 之后通过代码初始化collectionView

NSArray* visibleCellIndex = self.myCollectionView.indexPathsForVisibleItems;
Run Code Online (Sandbox Code Playgroud)