Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create customized UICollectionView with 2 or more custom cells?

in my project I want to use UICollectionView with custom cells, I created collection view with custom cells but I want to use Different size of custom cell in my project I followed some tutorials but I din`t get it properly, and below I attached sample screen shot of what i really looking for collection view.
Example image enter image description here

like image 502
Shanmugasundharam Avatar asked Jul 30 '15 18:07

Shanmugasundharam


2 Answers

One of the possible ways to create this is to use sizeForItemAtIndexPath and then return the size for your Cell. Here are some useful links on Github which are exactly doing what you want :

  1. RF Quilt Layout
  2. Mosaic Layout

As in the First image, some cells have buttons whereas others don't have. For this, you will have to create custom Cells i.e one custom cell with buttons and one without buttons. And inside your cellForItemAtIndexPath function, you can define them with some if-else condition.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   if(firstCellConditionMet)
    {
        CustomCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];

        //Your code

        return cell;
    }
    else{
        CustomCell2 *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier2" forIndexPath:indexPath];

         //Your Code

         return cell2;
        }
    }
}
like image 89
Munahil Avatar answered Oct 02 '22 12:10

Munahil


This can be achieved by Collection View Flow Layout. You can create a flow layout and that layout will take care of different rows and how many elements are there in a row. Have a look at Ray Wenderlich's Tutorial here.

like image 43
Gautam Jain Avatar answered Oct 02 '22 13:10

Gautam Jain