我有一个带有两个UICollectionViews的视图。这些视图中的每一个都有一个支持不同大小的数组。 collection1由array1支持,collection2由array2支持。问题是,从numberOfItemsInSection为collection1返回的任何数字都应用于两个集合视图。
例如,如果array1的大小为4,array2的大小为5,则两个集合都将显示4个元素。如果array1的大小为5而array2的大小为4,那么当我一直滚动collection2时,它会调用cellForItemAtIndexPath,itemIndex为5,对于collection2,我得到一个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 repo中添加了一个说明问题的项目。
git@github.com:civatrix / MultipleCollectionViews.git
答案 0 :(得分:20)
问题在于我为每个集合使用相同的布局对象。回想起来,但你必须确保为每个collectionView创建不同的布局。
答案 1 :(得分:4)
可能使用ContainerViews会更容易,并且每个UICollectionView都有两个独立的UICollectionView控制器
答案 2 :(得分:1)
你有什么应该工作。 self.collection 1和self.collection 2 IBOutlets?如果是这样,你能仔细检查它们是否正确连接?