如何在xcode中的同一页面内制作两个collectionView?

时间:2014-02-28 13:22:31

标签: ios uicollectionviewcell collectionview

我希望在同一页面中有两个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;

}

5 个答案:

答案 0 :(得分:6)

您可以通过区分'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;

    }
}

希望它有所帮助。

答案 1 :(得分:4)

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

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

您可以通过多种方式执行此操作,具体取决于UICollectionView的创建方式。

<强> 1。如果您在Storyboard或Interface Builder中创建它们,请使用IBOutlet将它们链接到您的班级。然后您可以使用以下代码区分它们:

if (collectionView == self.firstCollectionView) { ... }
else if (collectionView == self.secondCollectionView) { ... }

firstCollectionViewsecondCollectionView都必须是View Controller类中的属性。

<强> 2。另一种方法是在Storyboard / Interface Builder或具有tag属性的代码中为它们设置标记。

所以你可以这样做:

self.firstCollectionView.tag = 100;
self.secondCollectionView.tag = 200;

然后再次在cellForItemAtIndexPath方法中,您可以区分它们:

if (collectionView.tag == 100) { ... }
else if (collectionView.tag == 200) { ... }

即使您在代码中创建了集合视图,也可以使用相同的技术。创建强烈指向集合的属性,并比较指针或按标记进行比较。

答案 2 :(得分:1)

cellForItemAtIndexPath的委托方法也传递了它请求单元格的集合视图的实例。此代码段正在更正您在原始帖子中放置的代码示例。举个例子,这是你如何做到这一点:

- (void)viewDidLoad {

    // Initialization of collectionView done previously, set delegates to yourself.
    [self.collectionView1 setDelegate:self];
    [self.collectionView2 setDelegate:self];
}

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

    // Setting the cellIdentifier to "hall2" by default, no need to check twice.
    NSString *cellIdentifier = @"hall2";
    // The collectionView requesting a cell is collectionView2, change the cellIdentifier
    if ([collectionView isEqual:self.collectionView2]) {

        cellIdentifier = @"hall3";
    }

    // Since the collectionView we need is already passed, just dequeue and reuse from it.
    timeCell *cell = (timeCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

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

    return cell;

}

我希望这可以帮助您了解如何使用同一个委托使用多个集合视图。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

为了在同一页面中添加两个UICollectionView,我使用collectionView.tag来识别它们中的每一个,并决定将数据传递给每一个。 此外,我需要为每个集合视图定义不同的单元格大小,但在此方法中collectionView.tag不适合我。所以我必须为每个UICollectionViewLayout定义不同的UICollectionView,然后:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionViewLayout == layout1) {
        return CGSizeMake(100,100);
    } else {
        return CGSizeMake(50, 50);
    }
}