UICollectionView当前可见单元格索引 [英] UICollectionView current visible cell index

查看:1159
本文介绍了UICollectionView当前可见单元格索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPad应用程序中第一次使用 UICollectionView
我设置了 UICollectionView 这样它的大小和单元格大小相同,意味着一次只显示一个单元格。

I am using UICollectionView first time in my iPad application. I have set UICollectionView such that its size and cell size is same, means only once cell is displayed at a time.

问题:
现在,当用户滚动UICollectionView时,我需要知道哪个单元格可见,我必须更新其他UI元素。我没有找到任何委托方法。我怎样才能做到这一点?

Problem: Now when user scroll UICollectionView I need to know which cell is visible I have to update other UI elements on change. I didn't find any delegate method for this. How can I achieve this?

代码:

[self.mainImageCollection setTag:MAIN_IMAGE_COLLECTION_VIEW];
[self.mainImageCollection registerClass:[InspirationMainImageCollectionCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.mainImageFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.mainImageFlowLayout setMinimumInteritemSpacing:0.0f];
[self.mainImageFlowLayout setMinimumLineSpacing:0.0f];
self.mainImageFlowLayout.minimumLineSpacing = 0;
[self.mainImageCollection setPagingEnabled:YES];
[self.mainImageCollection setShowsHorizontalScrollIndicator:NO];
[self.mainImageCollection setCollectionViewLayout:self.mainImageFlowLayout];

我尝试了什么:

As UICollectionView 符合 UIScrollView ,当用户滚动以 UIScrollViewDelegate 方法

As UICollectionView Conforms to UIScrollView, I got when user scroll ends with UIScrollViewDelegate method

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

但是在上面的函数里面如何获得 UICollectionView

But inside above function how can I get current visible cell index of UICollectionView ???

推荐答案

方法[collectionView visibleCells]为您提供所需的所有visibleCells数组。当你想要的时候使用它

The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
        NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
        NSLog(@"%@",indexPath);
    }
}

这篇关于UICollectionView当前可见单元格索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆