Ios 如何在同一个Viewcontroller中保留多个uicollectionview控件

Ios 如何在同一个Viewcontroller中保留多个uicollectionview控件,ios,uicollectionview,Ios,Uicollectionview,我想在同一个Viewcontroller中保留多个uicollectionview控件。pl建议我使用代码或任何链接您需要设置两个集合视图的“标记”值,然后使用以下代码检查标记值: if (collectionView.tag == 0) { // collection view 1 } else if (collectionView.tag == 1) { // collection view 2 } 您可以在界面生成器或代码中设置标记值。使用setTag:方法。这只是一个想

我想在同一个Viewcontroller中保留多个uicollectionview控件。pl建议我使用代码或任何链接

您需要设置两个集合视图的“标记”值,然后使用以下代码检查标记值:

if (collectionView.tag == 0) {
    // collection view 1
}
else if (collectionView.tag == 1) {
    // collection view 2
}
您可以在界面生成器或代码中设置标记值。使用
setTag:
方法。

这只是一个想法

像这样初始化:

 CGRect mainFrame = self.view.frame;


 UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    collectionView1=[[UICollectionView alloc] initWithFrame:CGRectMake(0, mainFrame.origin.y , 320, 250) collectionViewLayout:layout];

    [collectionView1 setDataSource:self];
    [collectionView1 setDelegate:self];

    [collectionView1 registerClass:[Cell class] forCellWithReuseIdentifier:@"cellIdentifier"];

    [collectionView1 setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:collectionView1];

    [collectionView1 reloadData];



    UICollectionViewFlowLayout *layout2=[[UICollectionViewFlowLayout alloc] init];

    collectionview2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, mainFrame.origin.y +  270, 320, mainFrame.size.height-280) collectionViewLayout:layout2];

    [collectionview2 setDataSource:self];
    [collectionview2 setDelegate:self];

    [collectionview2 registerClass:[Cell2 class] forCellWithReuseIdentifier:@"destCellIdetifier"];

    [collectionview2 setBackgroundColor:[UIColor lightGrayColor]];

    [self.view collectionview2];

    [collectionview2 reloadData];
编写数据源和委托,如下所示

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == collectionView1) {
        return 18;
    }
    else
        return 8;
}


// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cell *cell1 = nil;

    if ([collectionView isEqual:collectionView1]) {
        cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        cell1.label.text = [NSString stringWithFormat:@"%d",indexPath.item];
    }
    else {

        Cell2 *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"destCellIdetifier" forIndexPath:indexPath];
        cell2.label.text = [NSString stringWithFormat:@"%d",indexPath.item];
        return cell2;

    }

    return cell1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(65, 60);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    return UIEdgeInsetsMake(10.0f, 10, 10.0f, 10.0f);
}

为两个集合控件提供不同的标记,并在委托方法中按标记检查特定集合控件。使用此选项我想添加多个uicollectionview,如子视图What is mainframe哪个变量