本文介绍了在 ViewController 中创建 Multi UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是我的问题.

我在 for 循环 中创建了 UICollectionView.所以,它创建了一些 UICollectionView

I had created UICollectionView in for loop. So, it's created some UICollectionView

我的代码:

for (int i = 0; i < getAllCategory.count; i ++) {
        CGRect frame;
        frame.origin.x = self.scroll.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scroll.frame.size;

        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 以显示新图像时,它不会显示新图像.因为,它是为最后一个 UICollectionView 重新加载数据.

So, When I scroll to new UICollectionView to show new image, it's not show new image. Because, it's reload data for last UICollectionView.

显示新图片时的代码.

- (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 / 2) / 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];
        }
    }
}

所以,它只显示所有 UICollectionView 的旧图像,它只显示最后一个 UICollectionView

So, it's only show old image for all UICollectionView, it's only change image for last UICollectionView

我该如何解决这个问题.

How to I can resolve this problem.

这是您的链接,您可以看到一些图片我的问题http://imgur.com/a/nIMic

Here is link to you see some images my problemhttp://imgur.com/a/nIMic

请帮帮我!

**************************** 我已经解决了我的问题 *******************

************************** I had resolved my problem *******************

推荐答案

添加代码

@property (strong, nonatomic) NSMutableArray *collectionArray;

self.collectionArray = [[NSMutableArray alloc]init]; 

[self.collectionArray addObject:collectionView];

[self.collectionArray[page] reloadData]; 

这篇关于在 ViewController 中创建 Multi UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:27