一个控制器中有多个UICollectionView [英] Multiple UICollectionView in one controller

查看:98
本文介绍了一个控制器中有多个UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个UICollectionViews的视图。这些视图中的每一个都有一个支持不同大小的数组。 collection1由array1支持,collection2由array2支持。问题是,从numberOfItemsInSection为collection1返回的任何数字都应用于两个集合视图。

I have a view set up with two UICollectionViews. Each of these views has an array backing it with different sizes. collection1 is backed by array1, and collection2 is backed by array2. The problem is, what ever number is returned for collection1 from numberOfItemsInSection is being applied to both collection views.

例如,如果array1的大小为4,array2的大小为5,两个系列都将展示4个元素。如果array1的大小为5,array2的大小为4,那么当我滚动collection2时,它会调用cellForItemAtIndexPath,itemIndex为5,对于collection2,我得到一个NSRangeException。

For instance, if array1 is size 4 and array2 is size 5, both collections will show 4 elements. If array1 is size 5 and array2 is size 4, when I scroll collection2 all the way it calls cellForItemAtIndexPath with an itemIndex of 5 for collection2 and I get an NSRangeException.

我可以让每个collectionView使用它自己的大小吗?

How can I make each collectionView use it's own size?

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

I've included a git repo with a project illustrating the problem.

git@github.com:civatrix/ MultipleCollectionViews.git

git@github.com:civatrix/MultipleCollectionViews.git

推荐答案

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

The problem was that I was using the same layout object for each collection. In retrospect that makes sense, but you have to make sure you create different layouts for each collectionView.

这篇关于一个控制器中有多个UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆