Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display different amount of cells in a row within a UICollectionView

I would like to have a UICollectionView that, in the first row there is a cell with all the row's width, and the second row has 3 cells. I have researched the documentation but I'm not able to do it. Something like this:

enter image description here

Thanks in advance

like image 882
Juanjo Avatar asked Feb 08 '14 18:02

Juanjo


People also ask

How do I have more than one type of cell in a collection view?

Alternatively, you can drag more than one cell from the Object Library into your collection view, assign a subclass and reuse identifier for the cell and design it as needed. Just like you would for a single cell.

What is Uicollectionviewflowlayout?

A layout object that organizes items into a grid with optional header and footer views for each section.


1 Answers

Make 2 Cell prototypes in collection view in your storyboard, and set them reuse identifiers

In your ViewController's .m file implement this functions

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{        
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

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

    if (/*condition for small cell*/)// e.g.    indexPath.row % 3 != 0
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallCell" forIndexPath:indexPath];
    else
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bigCell" forIndexPath:indexPath];


    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (/*condition for small cell*/) // e.g.    indexPath.row % 3 != 0
        return CGSizeMake(65, 50);

    return CGSizeMake(318, 50);
}

After doing this you will have something like this:

enter image description here

like image 178
arturdev Avatar answered Oct 22 '22 17:10

arturdev