简体   繁体   English

UICollectionViewCell宽度大于UICollectionView错误处理

[英]UICollectionViewCell width greater than UICollectionView error handling

I'm currently using a UICollectionView and a UICollectionViewFlowLayout and the following UICollectionViewCell : 我目前正在使用UICollectionViewUICollectionViewFlowLayout以及以下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? 所以现在的问题是,处理这种极端情况的最佳/正确方法是什么?

  1. Is there something small that I have missed? 有什么我错过的小东西吗?
  2. Given this scenario I would have to 'Tweak' the flow layout, so would it be recommended to subclass the flow layout, override 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.

相关问题 如果最后一个UICollectionViewCell的总宽度大于UICollectionView的宽度,则剪辑它们 - Clip last UICollectionViewCell if their total width is greater the UICollectionView width 使 UICollectionViewCell 与 UICollectionView 宽度相同 - Make UICollectionViewCell same width as UICollectionView UICollectionView和UICollectionViewCell IndexPath错误 - UICollectionView and UICollectionViewCell IndexPath error UICollectionViewCell y比UICollectionview高 - UICollectionViewCell higher y than UICollectionview 以编程方式将 UICollectionViewCell 宽度设置为 UICollectionView 宽度 - Set UICollectionViewCell width programmatically to UICollectionView width UICollectionViewCell 中的 UICollectionView - UICollectionView in a UICollectionViewCell Xamarin iOS 8,UICollectionView-使可重用的UICollectionViewCell出队时出错 - Xamarin iOS 8, UICollectionView - Error in dequeue reusable UICollectionViewCell 使用与其UICollectionView不同的Alpha值加载UICollectionViewCell - Load a UICollectionViewCell with different alpha value than its UICollectionView UITableViewCell宽度大于UITableView宽度 - UITableViewCell width is greater than UITableView width 将UIWebView集成到UICollectionView(UIcollectionViewCell)中 - Integrating an UIWebView in a UICollectionView(UIcollectionViewCell)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM