UICollectionViewestimatedItemSize - 最后一个单元格未对齐 [英] UICollectionView estimatedItemSize - last cell is not aligned

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

问题描述

我想在单元格中制作一个通常的水平滚动 flowLayout UICollectionView,其中包含估计的ItemSize 和 PreferredLayoutAttributesFittingAttributes.但是最后一个单元格有问题.知道问题出在哪里吗?项目本身

I want to make a usual horizontalScrolling flowLayout UICollectionView with estimatedItemSize and preferredLayoutAttributesFittingAttributes in cell. 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- 给出适当的最小项目间距来解决问题.

I face a similar issue and the problem was solved by giving a proper minimum inter item spacing, using the delegate methods - minimumInteritemSpacingForSectionAt- of UICollectionViewDelegateFlowLayout.

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

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