UICollectionViewCell 宽度大于 UICollectionView 错误处理

UICollectionViewCell width greater than UICollectionView error handling

我目前正在使用 UICollectionViewUICollectionViewFlowLayout 以及以下 UICollectionViewCell:

但是,最近出现了一个锁定 UI 的错误,并且在检查时控制台不断向控制台输出以下内容。

Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.The behavior of the UICollectionViewFlowLayout is not defined because: the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.

现在错误非常简单,单元格本身的长度大于集合视图的长度。这样做的原因是,在异常情况下,有一段数据非常长,导致单元格超出了集合视图的范围。

在单元格本身,我的行大小为 1,并且尾部被截断了。我假设自动布局会以这样的方式处理这个问题,即如果单元格大于集合视图宽度,它只会使用单元格上集合视图的宽度并截断标签中的文本。但是,autolayout 不会这样做,并且会在控制台中重复出现上述错误。

所以现在的问题是处理这种极端情况的最佳/正确方法是什么?

  1. 有什么我遗漏的小事吗?
  2. 鉴于这种情况,我将不得不 'Tweak' 流式布局,因此是否建议将流式布局子类化,覆盖 layoutAttributesForElementsInRect: 然后调整单元格大小 frame/size它大于 UICollectionView 的界限?这条路线似乎结束了这个错误?

向自定义 UICollectionViewCell 添加小于或等于宽度限制。

为该约束创建一个出口并将其设置在 collectionView(_:cellForItemAt:)

您可以只使用以下 UICollectionViewDelegateFlowLayout 方法来指定给定 indexPath

处项目的大小
func collectionView(_ collectionView: UICollectionView, 
                  layout collectionViewLayout: UICollectionViewLayout, 
           sizeForItemAt indexPath: IndexPath) -> CGSize

有关详细信息,请查看 Apple’s documentation