在ViewController中创build多个UICollectionView

标题是我的问题。

我在for loop创build了UICollectionView 。 所以,它创build了一些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的数据。

我的代码显示新的图像时。

 - (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更改图像

如何解决这个问题。

这里是链接到你看到一些图片我的问题http://imgur.com/a/nIMic

请帮帮我!

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

添加代码

 @property (strong, nonatomic) NSMutableArray *collectionArray; self.collectionArray = [[NSMutableArray alloc]init]; [self.collectionArray addObject:collectionView]; [self.collectionArray[page] reloadData]; 

你的代码只是处理一个collectionView (最后一个集合)。

你应该创build一个属性来保存这些collectionViewNSMutableArray * 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]; } 

然后在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; } } } } } 

请考虑你的devise模型,你可以分开不同类的UICollectionView,每个指向对象的指针,创build不同的数组和数据源来加载。