Create Multi UICollectionView in a ViewController

2019-05-30 14:12发布

问题:

Title is my problem.

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

My code:

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

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.

My code when show new image.

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

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.

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

Please help me!

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

回答1:

Add code

@property (strong, nonatomic) NSMutableArray *collectionArray;

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

[self.collectionArray addObject:collectionView];

[self.collectionArray[page] reloadData]; 


回答2:

Your code just handles a collectionView (the last collection).

You should create a property to hold these collectionView: NSMutableArray * arrCollectionView

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

            [arrCollectionView addObject: collectionView];
     }

Then in scrollViewDidScroll

- (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];
            for (UICollectionView * collectionView in arrCollectionView) {
                if (collectionView == sender) {
                     [collectionView reloadData];
                     break;
                }
            }
        }
    }
}


回答3:

Please thinking your design model, you can separate the UICollectionView in different Class, every pointer to object by self, create different array and datasource for load.