Santosh Maharjan Santosh Maharjan - 1 month ago 12
iOS Question

how to make two collectionView within same Page in xcode?

I want to have two

UICollectionView
in same page. Two
UICollectionView
that would show different data as required. How to do it?
thank you 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;

}

Answer

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.