2つのUICollectionViewでビューを設定しました。これらのビューのそれぞれには、サイズの異なる配列が配列されています。 collection1はarray1によってサポートされ、collection2はarray2によってサポートされます。問題は、numberOfItemsInSectionからcollection1に返される数値が両方のコレクションビューに適用されていることです。1つのコントローラに複数のUICollectionViewを表示
たとえば、array1のサイズが4で、array2のサイズが5の場合、両方のコレクションに4つの要素が表示されます。 array1がサイズ5でarray2がサイズ4の場合、collection2をスクロールすると、collection2のitemIndexが5のcellForItemAtIndexPathが呼び出され、NSRangeExceptionが返されます。
各コレクションビューに独自のサイズを使用するにはどうすればよいですか?
- (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を追加しました。
[email protected]:civatrix/MultipleCollectionViews.git
これは正しいです。私も同じ問題がありました。 –
ありがとう!これも私の問題を解決しました! – ordinaryman09
アップしてありがとう!私も解決しました! – z33