UICollectionView:页面控制的当前索引path

我正在使用stream布局的UICollectionView来显示单元格列表,我也有一个页面控件来指示当前页面,但似乎没有办法获得当前的索引path,我知道我可以得到可见的单元格:

UICollectionView当前可见的单元格索引

但是可以有多个可见的单元格,即使我的每个单元格都占据了整个屏幕的宽度,如果将它滚动为两个单元格的两半,那么它们都是可见的,那么是否有办法只获取一个当前可见单元格的索引?

谢谢

您可以通过监视scrollViewDidScroll委托中的contentOffset来获取当前索引

这将是这样的事情

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { NSInteger currentIndex = self.collectionView.contentOffset.x / self.collectionView.frame.size.width; } 

从视图中心通过NSIndexPath获取页面。

即使你的页面不等于UICollectionView的宽度。

  func scrollViewDidScroll(scrollView: UIScrollView) { let center = CGPoint(x: scrollView.contentOffset.x + (scrollView.frame.width / 2), y: (scrollView.frame.height / 2)) if let ip = collectionView.indexPathForItemAtPoint(center) { self.pageControl.currentPage = ip.row } } 

当滚动移动停止时,肯定需要抓住可见的项目。 使用下一个代码来做到这一点。

 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { if let indexPath = myCollectionView.indexPathsForVisibleItems.first { myPageControl.currentPage = indexPath.row } } 
  1. PageControl放置在您的视图中或由代码设置。
  2. 设置UIScrollViewDelegate
  3. Collectionview – > cellForItemAtIndexPath(Method)中添加下面的代码来计算页数,

     int pages = floor(ImageCollectionView.contentSize.width/ImageCollectionView.frame.size.width); [pageControl setNumberOfPages:pages]; 
  4. 添加ScrollView委托方法,

     #pragma mark - UIScrollVewDelegate for UIPageControl - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth = ImageCollectionView.frame.size.width; float currentPage = ImageCollectionView.contentOffset.x / pageWidth; if (0.0f != fmodf(currentPage, 1.0f)) { pageControl.currentPage = currentPage + 1; } else { pageControl.currentPage = currentPage; } NSLog(@"finishPage: %ld", (long)pageControl.currentPage); } 

我有类似的情况,我的stream布局设置为UICollectionViewScrollDirectionHorizo​​ntal,我正在使用页面控制来显示当前页面。

我使用自定义stream布局来实现它。

/ ————————自定义标头的头文件(.h)—————- ——– /

 /** * The customViewFlowLayoutDelegate protocol defines methods that let you coordinate with *location of cell which is centered. */ @protocol CustomViewFlowLayoutDelegate <UICollectionViewDelegateFlowLayout> /** Informs delegate about location of centered cell in grid. * Delegate should use this location 'indexPath' information to * adjust it's conten associated with this cell. * @param indexpath of cell in collection view which is centered. */ - (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout cellCenteredAtIndexPath:(NSIndexPath *)indexPath; @end @interface customViewFlowLayout : UICollectionViewFlowLayout @property (nonatomic, weak) id<CustomViewFlowLayoutDelegate> delegate; @end 

/ ——————-自定义标题的实现文件(.m)——————- /

 @implementation customViewFlowLayout - (void)prepareLayout { [super prepareLayout]; } static const CGFloat ACTIVE_DISTANCE = 10.0f; //Distance of given cell from center of visible rect static const CGFloat ITEM_SIZE = 40.0f; // Width/Height of cell. - (id)init { if (self = [super init]) { self.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.minimumInteritemSpacing = 60.0f; self.sectionInset = UIEdgeInsetsZero; self.itemSize = CGSizeMake(ITEM_SIZE, ITEM_SIZE); self.minimumLineSpacing = 0; } return self; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; CGRect visibleRect; visibleRect.origin = self.collectionView.contentOffset; visibleRect.size = self.collectionView.bounds.size; for (UICollectionViewLayoutAttributes *attribute in attributes) { if (CGRectIntersectsRect(attribute.frame, rect)) { CGFloat distance = CGRectGetMidX(visibleRect) - attribute.center.x; // Make sure given cell is center if (ABS(distance) < ACTIVE_DISTANCE) { [self.delegate collectionView:self.collectionView layout:self cellCenteredAtIndexPath:attribute.indexPath]; } } } return attributes; } 

包含集合视图的类必须符合前面在自定义布局头文件中描述的协议“CustomViewFlowLayoutDelegate”。 喜欢:

 @interface MyCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegate, CustomViewFlowLayoutDelegate> @property (strong, nonatomic) IBOutlet UICollectionView *collectionView; @property (strong, nonatomic) IBOutlet UIPageControl *pageControl; .... .... @end 

有两种方法可以将自定义布局绑定到集合视图,无论是在xib中,还是在像viewDidLoad中说的代码中:

 customViewFlowLayout *flowLayout = [[customViewFlowLayout alloc]init]; flowLayout.delegate = self; self.collectionView.collectionViewLayout = flowLayout; self.collectionView.pagingEnabled = YES; //Matching your situation probably? 

最后,在MyCollectionViewController实现文件中,实现“CustomViewFlowLayoutDelegate”的委托方法。

 - (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout cellCenteredAtIndexPath:(NSIndexPath *)indexPath { self.pageControl.currentPage = indexPath.row; 

}

我希望这会有所帮助。 🙂