问题
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 :
- RF Quilt Layout
- 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