Search code examples
iosuiviewcontrolleruicollectionviewcellcollectionview

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


I want to have two UICollectionView in same page. Two UICollectionView that would show different data as required. How can I do this? Thanks in advance.

- (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;

}

Solution

  • You can do the same by differentiating in '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;
    
        }
    }
    

    Hope it helps.