[英]UICollectionViewCell width greater than UICollectionView error handling
I'm currently using a UICollectionView
and a UICollectionViewFlowLayout
and the following UICollectionViewCell
: 我目前正在使用
UICollectionView
和UICollectionViewFlowLayout
以及以下UICollectionViewCell
:
However, a bug was raised recently that locks the UI and upon inspection the console kept spitting out the following continuously to the console. 但是,最近引发了一个错误,该错误锁定了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.
在UICollectionViewFlowLayoutBreakForInvalidSizes处创建一个符号断点,以在调试器中捕获此断点。未定义UICollectionViewFlowLayout的行为,因为:项目宽度必须小于UICollectionView的宽度减去部分插入左右值,减去内容插入左和右正确的价值观。
Now the error is pretty straight forward, the cell itself has a length greater than that of the collection view. 现在错误非常简单明了,单元格的长度大于集合视图的长度。 The reason for this is that in an outlier occurrence, there is a piece data that is just very long and causes the cell to go outside the bounds of the collection view.
其原因是,在异常情况下,存在一个非常长的片段数据,导致单元格超出了收集视图的范围。
On the cell itself, I have a line size of 1 and that the tail is truncated. 在单元格本身上,我的行大小为1,并且尾巴被截断了。 It was my assumption that autolayout would handle this in such a way that if the cell is greater than the collection view width it would just use the width of the collection view on the cell and just truncate the text in the label.
我假设自动布局将以这种方式处理该问题,即如果单元格大于集合视图的宽度,它将仅使用单元格上集合视图的宽度,然后截断标签中的文本。 However, autolayout does not do this and the error above is produced repetitively in the console.
但是,自动布局不会执行此操作,并且在控制台中反复产生上述错误。
So now the question is what would be the best / correct way to handle this edge case? 所以现在的问题是,处理这种极端情况的最佳/正确方法是什么?
layoutAttributesForElementsInRect:
and then adjust that cells size frame/size whenever it is greater than the bounds of the UICollectionView
? layoutAttributesForElementsInRect:
然后每当它大于UICollectionView
的边界时,就调整该单元格的大小。 This route seems like over kill the bug? Add a less-than-or-equal width constraint to your custom UICollectionViewCell
. 向您的自定义
UICollectionViewCell
添加小于或等于的宽度约束。
Create an outlet to that constraint and set it in collectionView(_:cellForItemAt:)
. 创建该约束的出口,并将其设置在
collectionView(_:cellForItemAt:)
。
You may just use the following UICollectionViewDelegateFlowLayout method to dictate the size of the item at the given indexPath 您可以只使用以下UICollectionViewDelegateFlowLayout方法来指定给定indexPath处项目的大小。
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
For further details have a look at Apple's documentation 有关更多详细信息,请参阅Apple文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.