如何在一个Viewcontroller上添加两个CollectionView? [英] How do I add two CollectionViews on one Viewcontroller?

查看:155
本文介绍了如何在一个Viewcontroller上添加两个CollectionView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终结果是在一个视图控制器上有2个集合视图。从不同的来源拉动以及一个应该垂直滚动而另一个垂直滚动。

The end result is to have 2 collection views on one view controller. Both pulling from different sources as well as one should scroll horizontally while the other vertically.

请告知如何以编程方式实现此目的。

Please advise on how to achieve this programatically.

推荐答案

之前我没有使用 UICollectionView ,但是它继承了从UIScrollView,我有机会它非常类似于 UITableView

I haven't used UICollectionView before, but as it inherits from UIScrollView, I'm taking a chance that it's pretty similar to UITableView.

当使用一个CollectionView时,我假设您必须设置 collectionView.delegate = self; collectionView.dataSource = self ,并且在 .h -file,确保您的类使用< UICollectionViewDelegate,UICollectionViewDataSource> 或类似的东西。当您将collectionView的委托设置为您自己的视图( self )时,您确保为collectionView提供的数据来自您自己的类,委托方法。我相信你已经知道了,因为这应该是一个单一的collectionView非常简单。

When using one CollectionView, I'm assuming you have to set collectionView.delegate = self; and collectionView.dataSource = self, and in the .h-file, make sure your class is using <UICollectionViewDelegate, UICollectionViewDataSource> or something similar. When you're setting the delegate of the collectionView to your own view (self), you're making sure that the data provided for the collectionView comes from your own class, in the delegate-methods. I'm sure you already know this, as that should be pretty straight forward with one single collectionView.

当你使用两个collectionViews时,你必须设置

When you are using two collectionViews, then you have to set

collection1.delegate = self;
collection2.delegate = self;
collection1.dataSource = self;
collection2.dataSource = self;

这将使这两个collectionViews都调用委托方法。
例如,委托方法 -collectionView:cellForItemAtIndexPath:将被调用两次。一次用于collection1,一次用于collection2。

This will in turn do so that both collectionViews will call the delegate methods. For instance, the delegate method -collectionView:cellForItemAtIndexPath: will be called twice. Once for collection1, and once for collection2.

为了确保他们获得发送给他们的正确数据,你应该在每个委托和dataSource的开头创建一个简单的检查。方法,如下所示:

To make sure they get the correct data sent to them, you should create a simple check in the beginning of every delegate and dataSource method, like this:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(collectionView == collection1)
    {
        //return cell for collection1
    }
    else
    {
        //return cell for collection2
    }
}

在这里,我正在检查是否 collectionView 等于 collection1 collection2 。委托方法提供 collectionView 作为它调用方法的UICollectionView,并且必须是两者中的任何一个。如果你已经为 collectionView 调用了一个collectionViews,那么这看起来很可疑,所以一定要在逻辑上命名它们。

Here, I'm checking if collectionView is equal to collection1 or collection2. The delegate methods are providing collectionView as the UICollectionView it is calling the method for, and that has to be either of the two. This can look suspicious if you have called one of your collectionViews for collectionView though, so be sure to name them logically.

这篇关于如何在一个Viewcontroller上添加两个CollectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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