我有一个 View 设置有两个 UICollectionView。这些 View 中的每一个都有一个支持不同大小的数组。 collection1 由 array1 支持,collection2 由 array2 支持。问题是,从 numberOfItemsInSection 为 collection1 返回的任何数字都应用于两个 Collection View 。
例如,如果 array1 的大小为 4,array2 的大小为 5,则两个集合都将显示 4 个元素。如果 array1 的大小为 5,而 array2 的大小为 4,当我一直滚动 collection2 时,它调用 cellForItemAtIndexPath,collection2 的 itemIndex 为 5,我得到一个 NSRangeException。
如何让每个 collectionView 使用它自己的尺寸?
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
if(view == self.colleciton1){
return self.array1.count;
} else if (view == self.collection2){
return self.array2.count;
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
if(cv == self.collection1){
CharacterCell *cell = [cv dequeueReusableCellWithReuseIdentifier:FIRST_CELL_IDENTIFIER forIndexPath:indexPath];
cell.label.text = self.array1[indexPath.item];
return cell;
} else if (cv == self.collection2){
EpisodeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:SECOND_CELL_IDENTIFIER forIndexPath:indexPath];
cell.label.text = self.array2[indexPath.item];
return cell;
}
return nil;
}
我在项目中包含了一个 git 存储库来说明问题。
git@github.com:civatrix/MultipleCollectionViews.git
最佳答案
问题是我为每个集合使用相同的布局对象。回想起来这是有道理的,但您必须确保为每个 collectionView 创建不同的布局。
关于ios - 一个 Controller 中的多个 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12850396/