carro carro - 5 months ago 21
iOS Question

UICollectionView into UICollectionViewCell

I need to add

UICollectionView
into
UICollectionViewCell
. It seems easy but I don't get it.

I don't get to know where I have to initialize
UICollectionView
for each
UICollectionViewCell
and where to program
UICollectionViewDataSource
and
UICollectionViewDelegate
of each
UICollectionView
.

The result would be a collection of products (first
collectionview
where each cell is a product) and a collection of images for each product like image gallery (second collection view where each cell is an image). It's the same as Zara app.

First collection view looks like this and have only vertical scroll.

enter image description here

When you select a product, cells change their size to full screen size and the collection view change vertical scrolling to horizontal scrolling. Now vertical scrolling is for the second collection view, the image gallery.




Second collection view looks like this

enter image description here





In storyboard I have
UICollectionViewController
with
UICollectionViewDataSource
,
UICollectionViewDelegate
and
UICollectionViewDelegateFlowLayout
implementations.

ProductsCollectionViewController.h

@interface ProductsCollectionViewController : UICollectionViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> {
NSMutableArray *products;
}


ProductsCollectionViewController.m

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return products.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"ProductCell";

ProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

Product *product = [products objectAtIndex:indexPath.row];
[cell setProduct:product];

return cell;
}


Product is a main
NSObject
that have some information and also an
array
of images.

Product.h

@interface Product : NSObject

@property NSInteger categoryID;
@property NSInteger productID;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSArray *images;





I think that what I have to do is create new
UICollectionView
programatically in each
ProductCollectionViewCell
with the
array
of images of the product but
ProductCollectionViewCell
don't recognize
UICollectionViewDataSource
and
UICollectionViewDelegate
and their implementation.

Answer

I did an example for this and put in on github. It demonstrates the use UICollectionView inside a UICollectionViewCell.

https://github.com/irfanlone/Collection-View-in-a-collection-view-cell