简体   繁体   中英

UICollectionViewCell width greater than UICollectionView error handling

I'm currently using a UICollectionView and a UICollectionViewFlowLayout and the following 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.

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.

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. 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 ? This route seems like over kill the bug?

Add a less-than-or-equal width constraint to your custom UICollectionViewCell .

Create an outlet to that constraint and set it in collectionView(_:cellForItemAt:) .

You may just use the following UICollectionViewDelegateFlowLayout method to dictate the size of the item at the given indexPath

func collectionView(_ collectionView: UICollectionView, 
                  layout collectionViewLayout: UICollectionViewLayout, 
           sizeForItemAt indexPath: IndexPath) -> CGSize

For further details have a look at Apple's documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM