0

I have a CollectionView with simple cells - a label and an image. Via AutoLayout I make sure the label is always on top of the image.

enter image description here

I use a 2-column layout, which produces the following view (debug mode / color blended layers):

enter image description here

In the CollectionViewController, when calling cellForItemAtIndexPath, I use dequeueReusableCellWithReuseIdentifier, which sets label text and imageview image to nil in the CollectionViewCell:

override func prepareForReuse() {
    coverImage.image = nil
    title.text = nil
    collection = nil
}

However, when re-attaching the images, somehow AutoLayout stops working properly. When I scroll down, to cells that are initially outside the bounds of the view, the label is sometimes invisible and sometimes twice as big as it should be.

enter image description here

enter image description here

This is reproduceable and happens always on the same cells, however the underlying data is exactly the same as when the cells are layed out correctly (the label.text value is not nil or something with 2 lines).

I have tried various approaches to fixing this (including Auto Layout in UICollectionViewCell not working and UICollectionViewCell Auto Layout), however the problem is persisting.

I'm using/testing with iOS 9.3 and 8.1, and Xcode 7.3.1.

benfwirtz
  • 271
  • 6
  • 16
  • 1
    Did you set specific height value of your label? – Vladislav Kovalyov Aug 15 '16 at 09:04
  • Thank you so much @VladislavKovalyov, I'm not sure how I did not try that yet (embarrassing!), but it works. I'm surprised though, it feels really random. It works for most cells but scrolling up and down there are a few (always the same ones) where it sometimes works and sometimes doesn't. – benfwirtz Aug 15 '16 at 10:50

0 Answers0