在 UICollectionView 中连续显示不同数量的单元格 [英] Display different amount of cells in a row within a UICollectionView
本文介绍了在 UICollectionView 中连续显示不同数量的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个 UICollectionView
,在第一行有一个具有所有行宽的单元格,第二行有 3 个单元格.我已经研究了文档,但我无法做到.像这样的:
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:
提前致谢
推荐答案
在故事板的集合视图中制作 2 个 Cell 原型,并将它们设置为重用标识符
Make 2 Cell prototypes in collection view in your storyboard, and set them reuse identifiers
在你的 ViewController 的 .m 文件中实现这个函数
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:
这篇关于在 UICollectionView 中连续显示不同数量的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文