我有一個視圖設置了兩個UICollectionViews。這些視圖中的每一個都有一個支持不同大小的數組。 collection1由array1支持,而collection2由array2支持。問題是,從numberOfItemsInSection中爲collection1返回的數字正在應用於這兩個集合視圖。在一個控制器中的多個UICollectionView
例如,如果是ARRAY1大小4和數組2是大小5,兩個集合將顯示4個元素。如果array1的大小是5而array2的大小是4,那麼當我滾動collection2的所有方式時,它會調用cellForItemAtIndexPath,其collectionIndex爲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回購。
[email protected].com:civatrix/MultipleCollectionViews.git
這是正確的。我有同樣的問題。 –
謝謝!這也解決了我的問題! – ordinaryman09
向上和謝謝!也解決了我的! – z33