ios - 一个 Controller 中的多个 UICollectionView

标签 ios uicollectionview

我有一个 View 设置有两个 UICollectionView。这些 View 中的每一个都有一个支持不同大小的数组。 collection1 由 array1 支持,collection2 由 array2 支持。问题是,从 numberOfItemsInSection 为 collection1 返回的任何数字都应用于两个 Collection View 。

例如,如果 array1 的大小为 4,array2 的大小为 5,则两个集合都将显示 4 个元素。如果 array1 的大小为 5,而 array2 的大小为 4,当我一直滚动 collection2 时,它调用 cellForItemAtIndexPath,collection2 的 itemIndex 为 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 存储库来说明问题。

git@github.com:civatrix/MultipleCollectionViews.git

最佳答案

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

关于ios - 一个 Controller 中的多个 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12850396/

相关文章:

iphone - 在 Phonegap + IOS 按钮中使用 iScroll 4 多次触发点击事件

ios - 如何正确使用 cancelLocalNotification?

ios - 如何设置 UIWindow 边界以支持 iOS 9 iPad 上的多任务处理?

ios - 如何在 iOS 中将 UILabel 的字体名称设置为 HelveticaNeue Thin?

ios - 如何在 UICollectionView 中隐藏一个部分

ios - UITableViewController VS UICollectionView

ios - 将 segue 添加到动态创建的单元格

ios - 在 UIView 下面添加 CALayer

ios - 是否有类似 "self.tableView.estimatedRowHeight"的收集单元格方法?

iOS:即使单元格可见,cellForItemAtIndexPath 也返回 nil