ios – 一个控制器中的多个UICollectionView

我有一个视图设置与两个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

解决方法

问题是我为每个集合使用相同的布局对象.回想起来有意义,但你必须确保为每个collectionView创建不同的布局.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...