Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView doesn't render cells

In my view I have a UITableView which contains cells with horizontal UICollectionView inside. The problem is that sometimes UICollectionViewCells are not rendered on device/simulator. At the same time I can see those cells in the View Hierarchy Debugger:

What I see in the View Hierarchy Debugger: enter image description here

What I see on a device: enter image description here

As you can see, cells (with images) exist and have correct frames and content (proved by the View Hierarchy Debugger). Nevertheless they are not rendered on device/simulator for some reason. What could be the cause of that and how could I fix it?

UPD: another example with "3D" view and bug on one cell, but not on the other

device screenshot: enter image description here

view hierarchy debugger: enter image description here

like image 999
Fyodor Volchyok Avatar asked May 29 '17 12:05

Fyodor Volchyok


People also ask

How UICollectionView works?

UICollectionView makes adding custom layouts and layout transitions, like those in Photos, simple to build. You're not limited to stacks and grids because collection views are customizable. You can use them to make circle layouts, cover-flow style layouts, Pulse news style layouts and almost anything you can dream up!

How to set collection view in Swift?

Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices. The foundation is now set up in the storyboard.

How do I add a section title in UICollectionView?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .

What is CollectionView?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content.


1 Answers

It turned out that my particular problem is related to UICollectionView assertion error on stale data because sometimes mentioned crash happens. Playing around [collectionViewLayout invalidateLayout] I finally fixed this.

Just to record what was NOT the cause (and what you should check if faced with the same problem):

  1. check if you update views in the main thread only
  2. check if all autolayout constants are set correctly and don't conflict
  3. check that your custom views' intrinsic content sizes are calculated correctly

If none of the above is the cause of issue try invalidateLayout somewhere around reloadData or inside of layoutSubviews or similar method.

like image 184
Fyodor Volchyok Avatar answered Oct 15 '22 16:10

Fyodor Volchyok