簡體   English   中英

如何使兩個集合視圖出現在同一個視圖控制器上?

[英]How to make two collection views appear on the same view controller?

我想在同一頁面中有兩個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;

}

您可以通過區分 '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;

    }
}

希望能幫助到你。

因為兩個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必須在您的視圖控制器類的屬性。

2. 另一種方法是在 Storyboard/Interface Builder 上或在具有tag屬性的代碼中為它們設置標簽。

例如,您可以這樣做:

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

然后再次在 cellForItemAtIndexPath 方法中,您可以像這樣區分它們:

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

即使您在代碼中創建了集合視圖,您也可以使用相同的技術。 創建強烈指向集合的屬性並比較指針或按標簽比較它們。

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;

}

我希望這可以幫助您了解如何在同一個委托中使用多個集合視圖。

您可以使用不同的值標記兩個集合視圖。

為了在同一頁面中添加兩個UICollectionView ,我使用 collectionView.tag 來標識它們中的每一個並決定將數據傳遞給每個。 此外,我需要為每個集合視圖定義不同的單元格大小,但在此方法中 collectionView.tag 對我不起作用。 所以我必須為每個UICollectionView定義一個不同的UICollectionViewLayout然后:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM