【问题标题】:UICollectionView estimatedItemSize - last cell is not alignedUICollectionView estimatedItemSize - 最后一个单元格未对齐
【发布时间】:2014-10-25 20:26:33
【问题描述】:

我想在单元格中制作一个通常的水平滚动 flowLayout UICollectionView,其中包含估计的ItemSize 和 PreferredLayoutAttributesFittingAttributes。但是最后一个单元格有问题。知道问题出在哪里吗? 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    

【问题讨论】:

  • 绝对令人困惑为什么会这样:S
  • 有这方面的消息吗?
  • 我遇到了同样的问题...解决了吗?
  • 有任何解决方案吗?
  • 我在您提供的链接上找不到您的演示?请再分享一次

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

我遇到了类似的问题,通过使用 UICollectionViewDelegateFlowLayout 的委托方法 - minimumInteritemSpacingForSectionAt- 给出适当的最小项目间距来解决问题。

【讨论】:

  • @artyx 如果这对您有帮助,请标记我的回答正确
【解决方案2】:

您正在视图本身的初始化中设置estimatedItemSize。

你需要在一些控制器中设置它。

还有,

如果所有单元格的高度相同,请使用 itemSize 属性而不是此属性来指定单元格大小。

文档:estimatedItemSize

【讨论】:

  • Apple 开发人员支持将我的问题确定为错误。
  • 有关于这个问题的消息吗?
  • 有关于这个错误的消息吗? xD
【解决方案3】:

有一个简单的方法可以解决这个问题。您可以添加原型单元的数量以检查所需位置的单元。在最后一个单元格中找到问题后。检查 Inspector 窗口中的单元格插图。

【讨论】:

    【解决方案4】:

    你调用了 super 方法,但是你没有使用 super 返回的 layoutAttributes。

        [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
    

    你可以尝试打印出原始的layoutAttributes vs super的layoutAttributes。 有时候,你不需要调用超级函数。

    其次,您可以创建自定义 flowlayout 或设置 inset 让您的单元格对齐顶部。我在我的项目中这样做了。

    【讨论】:

      【解决方案5】:

      您可以将其视为建议。据我所知,UICollectionView 的高度超过了UICollectionViewCell 高度,这就是它发生的原因。请让他们平等

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          我做过类似的小项目(一个原始(1*N)水平集合视图),这里是github。希望对您的需求有所帮助。

          https://github.com/texas16/HorizontalCollectionView

          【讨论】:

            【解决方案8】:

            有同样的问题,在我的情况下解决它的方法是确保:

            1. 所有单元格高度相等。

            2. collectionView 高度大于单元格高度 + 单元格之间的空间。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-10-13
              • 1970-01-01
              相关资源
              最近更新 更多