带有 ScrollView 的 IOS UICollectionView [英] IOS UICollectionView with ScrollView

查看:107
本文介绍了带有 ScrollView 的 IOS UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有 6 张图片.

I have 6 images in my database.

我创建了UIScrollView *scroll;,之后,我创建了UICollectionView 并将UICollectionView 添加到UIScrollView>

I have created UIScrollView *scroll;, after that, I had created UICollectionView and added UICollectionView to UIScrollView

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
        collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(screenWidth*i,5,screenWidth ,self.scroll.frame.size.height-5) collectionViewLayout:layout];
        [collectionView setDataSource:self];
        [collectionView setDelegate:self];
        [collectionView setPagingEnabled:YES];
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
        [collectionView setBackgroundColor:[UIColor purpleColor]];
        [self.scroll addSubview:collectionView];

现在,当滚动时,我想在此代码下方重新加载 UICollectionView 的数据:

And now, when scroll I want reload data of UICollectionView on below this code:

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (!pageControlBeingUsed) {
        [getAllEmoji removeAllObjects];
        // Switch the indicator when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scroll.frame.size.width;
        int page = floor((self.scroll.contentOffset.x - pageWidth / 3) / pageWidth) + 1;
        self.pageControl.currentPage = page;
        //Change Emoji when scroll
        NSString *str = [getAllCategory objectAtIndex:page];
        NSArray *arr = [Emoji MR_findByAttribute:@"category" withValue:str];
        if (arr.count > 0) {
            for (int i = 0; i < arr.count; i++) {
                Emoji *emoji = [arr objectAtIndex:i];
                [getAllEmoji addObject:emoji.name_emoji];
            }
            [collectionView reloadData];
        }
    }
}

第一页控件是 2 个图像.

first pageControl is 2 images.

当我滚动到第二个 pageControl 是 2 个图像(但是,它不加载新图像).

When I scroll to second pageControl is 2 images(But, it's don't load new image).

当我滚动到第三个 pageControl 是 2 个图像(它正在加载新图像).

When I scroll to third pageControl is 2 images(it's load new image).

您可以在以下链接中看到我的项目中的一些图片:http://imgur.com/a/nIMic

You can see some images in my project on below link: http://imgur.com/a/nIMic

还有我的项目演示.https://github.com/VMTrinh/Sticker

请帮帮我!

推荐答案

我猜你的问题是你不需要在滚动视图中嵌入集合视图.

I guess your problem is that you don't need to embed collection view in a scroll view.

滚动机制由集合视图管理.

The scroll mechanism is managed by the collection view.

您可以使用 scrollToItemAtIndexPath: 方法滚动您的集合.

You can scroll your collection with the scrollToItemAtIndexPath: method.

这篇关于带有 ScrollView 的 IOS UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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