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

无人久伴 提交于 2019-12-22 03:16:11

问题


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.


回答1:


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



回答2:


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.




回答3:


if it is what you want

https://www.youtube.com/watch?v=LFBTbmvFR30

try to use custom layout for collection view

https://github.com/bryceredd/RFQuiltLayout

this is ready decision and it it is easy to use, If there are questions, will answer you,now i do the very similar collection...but on iPad with random cell size>>>>

and the most cool, with this realization you will be able to use one class for all cell sizes, if you use constraints. If you have problem with this realization, i can give you my test project. Good luck bro.




回答4:


I think it's very easy. You can subclass UICollectionViewFlowLayout.



来源:https://stackoverflow.com/questions/31731675/how-to-create-customized-uicollectionview-with-2-or-more-custom-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!