2つのUICollectionViewで設定されたビューがあります。これらの各ビューには、さまざまなサイズの配列があります。collection1はarray1に支えられており、collection2はarray2に支えられています。問題は、numberOfItemsInSectionからcollection1に返される数値が、両方のコレクションビューに適用されていることです。
たとえば、array1がサイズ4で、array2がサイズ5の場合、両方のコレクションに4つの要素が表示されます。array1がサイズ5でarray2がサイズ4の場合、collection2をスクロールすると、collection2のitemIndexが5のcellForItemAtIndexPathが呼び出され、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