【问题标题】:Could not get UICollectionView current page无法获取 UICollectionView 当前页面
【发布时间】:2014-02-19 08:52:20
【问题描述】:

我使用以下代码在水平方向上对collection view 进行了分页。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView.pagingEnabled = YES;
    self.collectionView.directionalLockEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;

    [self setup];

    self.pageControl.currentPage = 0;

    self.pageControl.numberOfPages = floor(self.collectionView.contentSize.width /   self.collectionView.frame.size.width) + 1;
}

我使用pageControl 表示我有多少页和当前页。

我使用类似于以下链接的方法来确定当前页面。

iOS6 UICollectionView and UIPageControl - How to get visible cell?

UICollectionView: current index path for page control

如下。

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

但是,它不起作用。当我将collection view 滑动到下一页时,pageControl 仍然指向第一页。它不会改变它的值。

我打印出self.collectionView.contentOffset.x 值,它总是在框架内(这是第一页,即使我已经滑动到下一页),它永远不会转到下一页。

我是不是做错了什么?

【问题讨论】:

    标签: ios uicollectionview


    【解决方案1】:

    您是否考虑了单元格之间的间距?

    试试这个:

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        float currentPage = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
        self.pageControl.currentPage = ceil(currentPage);
    
        NSLog(@"Values: %f %f %f", self.collectionView.contentOffset.x, self.collectionView.frame.size.width, ceil(currentIndex));
    }
    

    【讨论】:

      【解决方案2】:

      斯威夫特 4.2 页面控件获取集合视图当前页面

      func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
      
              let pageWidth = scrollView.frame.size.width
              currentPage = Int((scrollView.contentOffset.x + pageWidth / 2) / pageWidth)
              pageControl.currentPage = currentPage
          }
      

      【讨论】:

        猜你喜欢
        • 2011-05-07
        • 2012-12-04
        • 2013-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-14
        • 2017-06-25
        相关资源
        最近更新 更多