如何使两个集合视图出现在同一视图控制器上?

问题描述 投票:2回答:5

我想在同一页面中有两个UICollectionView。两个UICollectionView将根据需要显示不同的数据。我怎样才能做到这一点?预先感谢。

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifierHall2 = @"hall2";

    /* Uncomment this block to use subclass-based cells */
    timeCell *cell = (timeCell *)[myCollectionViewHall2 dequeueReusableCellWithReuseIdentifier:cellIdentifierHall2 forIndexPath:indexPath];

    [cell.timeButton setTitle:[[allSimilarMutableArray valueForKey:@"showTime"] objectAtIndex:indexPath.item] forState:UIControlStateNormal];


    return cell;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifierHall3 = @"hall3";

    /* Uncomment this block to use subclass-based cells */
    timeCell *cell = (timeCell *)[myCollectionViewHall3 dequeueReusableCellWithReuseIdentifier:cellIdentifierHall3 forIndexPath:indexPath];

    [cell.timeButton setTitle:[[allSimilarMutableArray valueForKey:@"showTime"] objectAtIndex:indexPath.item] forState:UIControlStateNormal];


    return cell;

}
ios uiviewcontroller uicollectionviewcell collectionview
5个回答
7
投票

您可以通过区分'cellForItemAtIndexPath'来做同样的事情>>

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    if (collectionView == self.collectiveview1) {

         UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    [titleLabel setText:celldata];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:200];
    [imageView setImage:[UIImage imageNamed:connimage]];

    return cell;

    } else  {

         static NSString *cellIdentifier = @"FollowCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    [titleLabel setText:celldata];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:200];
    [imageView setImage:[UIImage imageNamed:cellImage]];

    return cell;

    }
}

希望有帮助。


4
投票

因为两个UICollectionView都响应相同的协议:UICollectionViewDataSource,所以必须在方法中区分它们:


1
投票

cellForItemAtIndexPath的委托方法还传递了为其请求单元格的集合视图的实例。此代码段正在纠正您在原始帖子中放置的代码示例。以您的示例为例,这是您的操作方式:


0
投票

您可以用不同的值标记两个集合视图。


0
投票

为了在同一页面中添加两个UICollectionView,我使用collectionView.tag来标识它们中的每个,并确定要传递给每个的数据。另外,我需要为每个集合视图定义不同的像元大小,但是在这种方法中,collectionView.tag对我不起作用。因此,我必须为每个UICollectionViewLayout定义一个不同的UICollectionView,然后:

© www.soinside.com 2019 - 2024. All rights reserved.