NSCollectionViewItem's custom view flickers onReloadData

338 Views Asked by At

I have a NSCollectionViewItem with a custom view, in that view I am adding custom CALayer to some items. On button action, I am calling NSCollectionView's reloadData() method to reload the different data. This causes the custom CALayer to flicker as the item changes. Any idea how to fix it?

Adding CALayer to item's view based on some condition

circleLayer = CALayer();
let dimension = floor(min(bounds.width, bounds.height) - 10);
circleLayer.frame = NSMakeRect(4.5, 4, dimension, dimension);
circleLayer.cornerRadius = dimension / 2;
backgroundLayer.backgroundColor = NSColor(red: 84/255, green: 84/255, blue: 87/255, alpha: 1.0).cgColor;
circleLayer.masksToBounds = false;
circleLayer.isHidden = false;
layer?.addSublayer(circleLayer);

Function called on button click

//loading different data
collectionView.reloadData();

So as the button is clicked the background color of some other item changes, but the background color of previous item flickers. I guess this probably due to reuse of NSCOllectionViewItem. How can I avoid this? Any idea?

0

There are 0 best solutions below