【问题标题】:Make UICollectionViewFlowLayout not centering cells on the same line使 UICollectionViewFlowLayout 不在同一行居中单元格
【发布时间】:2016-12-21 20:29:31
【问题描述】:

根据 Apple 文档:

If you want to specify different sizes for your cells, you must implement the collectionView:layout:sizeForItemAtIndexPath: method on the collection view delegate. You can use the provided index path information to return the size of the corresponding item. During layout, the flow layout object centers items vertically on the same line, as shown in Figure 3-2. The overall height or width of the line is then determined by the largest item in that dimension.

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW1

现在我有一个包含 2 列和高度不同的单元格的集合视图。问题是较小的单元格以较高的单元格在其左侧/右侧居中,但不会向上浮动,而其下方的单元格也会向上浮动。

我怎样才能做到这一点?

我有什么:

我想要什么:

似乎我应该继承 UICollectionViewFlowLayout,但是我应该如何实现呢?

【问题讨论】:

  • 您好,我遇到了类似的问题。我想知道您是分块加载数据还是一次加载数据?因为在 raywenderlich 教程中,他们正在一次计算整个数组。但我想分块加载数据(延迟加载)。

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

您想要的集合视图布局类型称为“瀑布布局”。实现有点棘手,因为您需要覆盖 UICollectionViewFlowLayout 的基本行为。

我建议您查看 this tutorial by Ray Wenderlich 以从头开始构建瀑布布局,或者 - 如果您想要一个已经打包的库 - 使用 GitHub 上的库,例如 CHTCollectionViewWaterfallLayoutWaterfallCollectionView 或 @987654324 @

【讨论】:

  • 感谢您的回复,因为我只能将一个标记为答案,所以我给了您一票
  • 谢谢@Edward,非常感谢=)
【解决方案2】:

您想要实现的称为“交错布局”。它是由 Pinterest 在他们的 iOS 应用中引入的。

您将需要自定义集合视图布局,this tutorial 将解释如何。或者,您可以使用现有解决方案之一,例如 CHTCollectionViewWaterfallLayout

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多