IOS UICollectionView with ScrollView

2019-05-19 08:59发布

I have 6 images in my database.

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];

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];
        }
    }
}

first pageControl is 2 images.

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

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

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

And my project demo. https://github.com/VMTrinh/Sticker

Please help me!

1条回答
戒情不戒烟
2楼-- · 2019-05-19 09:31

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.

You can scroll your collection with the scrollToItemAtIndexPath: method.

查看更多
登录 后发表回答