最后一个单元格的 UICollectionView 布局 [英] UICollectionView layout of last cell

查看:33
本文介绍了最后一个单元格的 UICollectionView 布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UICollectionViewFlowLayout 的 collectionview我在里面放了 10 个项目.它们有不同的尺寸:

I have a collectionview which has a UICollectionViewFlowLayout I put 10 items in it. and they have different sizes:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row<6) {
    CGSize retval =  CGSizeMake(100, 100);
    return retval;
}

CGSize retval =  CGSizeMake(200, 130);

return retval;
}

它的布局如下:前6个单元格看起来不错,7-9个是正确的,但最后一个没有居中对齐,

it layouts like: first 6 cells look good, and 7-9 are correct, but the last one is not center aligned,

谁能给我解释一下?

和怨恨的日子我在玩收藏布局,但仍然对选择感到困惑flowlayout 和自定义布局.对此有何原则?

and resent days Im playing with collection layouts, but still feel confused abt the selection of flowlayout and custom layout . any principles on this?

提前多谢.

推荐答案

我遇到了类似的问题,集合视图中的最后几个单元格有一个难看的偏移.就我而言,这是由于不遵守与项目大小相关的部分插图.

I came across a similar issue with the last few cells in the collection view having an unsightly offset. In my case it was a result of not honoring the section insets with respect to the item sizes.

尝试将 flowLayout 的 sectionInset 设置为 0:

Try setting the sectionInset of the flowLayout to 0:

flowLayout.sectionInset = UIEdgeInsetsZero;

如果这不起作用,您可能需要强制执行 minimumInterItemSpacing,因为流布局似乎在最后几个单元格上对此进行了干扰:

If this doesn't work you might need to enforce a minimumInterItemSpacing as the flow layout seems to bugger this up on the last few cells:

flowLayout.minimumInteritemSpacing = 10.f;//or any reasonable number

这篇关于最后一个单元格的 UICollectionView 布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆