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,其中包含一个说明问题的项目.

[email protected]:civatrix / MultipleCollectionViews.git

解决方法

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...