简体   繁体   English

UICollectionView estimatedItemSize - 最后一个单元格未对齐

[英]UICollectionView estimatedItemSize - last cell is not aligned

I want to make a usual horizontalScrolling flowLayout UICollectionView with estimatedItemSize and preferredLayoutAttributesFittingAttributes in cell. 我想在单元格中使用estimatedItemSize和preferredLayoutAttributesFittingAttributes制作一个通常的horizo​​ntalScrolling flowLayout UICollectionView。 But there is something wrong with last cell. 但最后一个细胞有问题。 Any idea where is the issue? 知道问题出在哪里? Project itself 项目本身

在此输入图像描述

@implementation RowCollectionView

- (instancetype) initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithFrame:frame collectionViewLayout:layout])
    {
        [self configureRowCollectionView];
    }

    return self;
}

- (void) awakeFromNib
{
    [super awakeFromNib];

    [self configureRowCollectionView];
}

- (void) configureRowCollectionView
{
    self.backgroundColor = [UIColor lightGrayColor];

    self.dataSource = self;
    self.delegate = self;

    // Horizontal Direction
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *) self.collectionViewLayout;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // Estimated Item Size
    flowLayout.estimatedItemSize = CGSizeMake(self.bounds.size.height, self.bounds.size.height);


    [self registerClass:[RowCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([RowCollectionViewCell class])];
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([RowCollectionViewCell class]) forIndexPath:indexPath];

    cell.contentView.backgroundColor = [UIColor redColor];

    return cell;
}

@end

@implementation RowCollectionViewCell

- (UICollectionViewLayoutAttributes *) preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super preferredLayoutAttributesFittingAttributes:layoutAttributes];

    UICollectionViewLayoutAttributes *attributes = [layoutAttributes copy];

    attributes.size = CGSizeMake(80, 80);

    return attributes;
}

@end    

我面临一个类似的问题,通过使用委托方法--UICollectionViewDelegateFlowLayout的minimumInteritemSpacingForSectionAt-给出一个适当的最小项间距来解决问题。

You are setting estimatedItemSize in the init of view itself. 您正在视图的init中设置estimatedItemSize。

You need to set it in some controller. 您需要在某个控制器中设置它。

Also, 也,

If all of your cells are the same height, use the itemSize property, instead of this property, to specify the cell size instead. 如果所有单元格的高度相同,请使用itemSize属性而不是此属性来指定单元格大小。

Documentation: estimatedItemSize 文档: estimatedItemSize

there is a simple method to resolve this. 有一个简单的方法来解决这个问题。 You can add number of prototype cells to check the cell at required position. 您可以添加多个原型单元格以检查所需位置的单元格。 Once you find the issue at last cell . 一旦在最后一个单元格中找到问题。 Check the cell insets in Inspector window. 检查“检查器”窗口中的单元格插图。

You call super method but you did not use super returned layoutAttributes. 你调用super方法,但你没有使用super返回的layoutAttributes。

    [super preferredLayoutAttributesFittingAttributes:layoutAttributes];

You can try to print out original layoutAttributes vs super's layoutAttributes. 您可以尝试打印出原始layoutAttributes vs super的layoutAttributes。 Sometimes, you don't need to call super function. 有时,您不需要调用超级函数。

Second, You can create custom flowlayout or set inset to let your cell align top. 其次,您可以创建自定义flowlayout或设置插入以让您的单元格对齐顶部。 I did this in my project. 我在我的项目中这样做了。

You can consider it a Suggestion. 您可以将其视为建议。 According to me the height of UICollectionView is more than UICollectionViewCell Height, thats why its happening. 根据我的说法, UICollectionView的高度超过了UICollectionViewCell Height,这就是它发生的原因。 please make them equal them 请让他们平等

自定义单元格大小必须与集合视图单元格大小相同,请检查。它可以为您解决问题。

I have done the similar small project (one raw (1*N) horizontal collection view), here is the github. 我做了类似的小项目(一个原始(1 * N)水平集合视图),这里是github。 I hope it would be helpful for your requirement. 我希望这对你的要求有所帮助。

https://github.com/texas16/HorizontalCollectionView https://github.com/texas16/Horizo​​ntalCollectionView

Had same problem and what fix it in my case was to make sure : 有同样的问题,在我的情况下修复它是为了确保:

  1. All cells height are equal. 所有细胞高度相等。

  2. The collectionView height is bigger then cell height + space between cells. collectionView高度大于单元格高度+单元格之间的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM