问题描述
在我的项目中我想使用自定义单元格的 UICollectionView
,我创建了自定义单元格的集合视图,但我想在我遵循的项目中使用不同大小的自定义单元格一些教程,但我没有得到它正确,下面我附上了我真正寻找集合视图的示例屏幕截图。
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.
推荐答案
创建此方法的一种可能方法是使用 sizeForItemAtIndexPath
然后返回 Cell
的大小。以下是Github上一些有用的链接,它们正是你想要的:
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
与第一张图片一样,有些单元格有按钮,有些则没有。为此,您必须创建自定义单元格,即一个带按钮的自定义单元格和一个没有按钮的单元格。在 cellForItemAtIndexPath
函数中,您可以使用一些 if-else
条件来定义它们。
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个或更多自定义单元格创建自定义UICollectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!