Shayan Jalil Shayan Jalil - 4 months ago 36
iOS Question

UICollectionViewCell strange behaviour. Cells not updating layer correctly

I have a UICollectionView with multiple cells on which I add a CAGradient layer representing the color of each cell. The problem is that when I push another view controller on top of the present view controller and then pop the second view controller, my collection view cells shift colors in a random order. To give you an idea I have attached screenshots.

This is the original order of the cells. This is correct
This is the original order of the cells. This is correct

This happens when I push another view controller and then return
This happens when I push another view controller and then return
You can see that the cells shifted their colors even though I changed nothing.

This is the code I use to initialize the cells.

[collectionview reloadData] is called in -viewWillAppear so the cells load every time the view appears

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
[self filterRecords];
MyCell *cell = (MyCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"ProspectCVCell" forIndexPath:indexPath];


for (int i = 0; i < [eventsSortedForAddedDate count]; i++)
{
Events* event = (Events*)[eventsSortedForAddedDate objectAtIndex:i];

if ([event.activityLevel.activityName isEqualToString:@"None"])
continue;

color = [[event activityLevel] color];

if (![color isEqualToString:@"#FFCCCCCC"])
break;
else
color = nil;
}


if (!([color length] > 0) || color == nil)
{
color = @"#FFCCCCCC";
}

UIColor* myColor = [self getUIColorObjectFromHexString:color alpha:.9];

//cell.backgroundColor = myColor;

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[myColor CGColor], (id)[[UIColor whiteColor] CGColor], nil];
gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.85f], nil];
//[cell.layer insertSublayer:gradient atIndex:0];
[cell.layer insertSublayer:gradient atIndex:0];


cell.prospectImageView.layer.shadowColor = [UIColor blackColor].CGColor;
cell.prospectImageView.layer.shadowOffset = CGSizeMake(0, 3.0);
cell.prospectImageView.layer.shadowRadius = 3.0;
cell.prospectImageView.layer.shadowOpacity = 0.8;

cell.cellLabel.text = p.displayName;

cell.layer.borderWidth = 0.5f;
cell.layer.borderColor = [UIColor whiteColor].CGColor;

return cell;
}


There is nothing wrong in the way I get the color, I have debugged multiple times and checked that the colors I get are the correct ones.
If I do

cell.backgroundColor = myColor;


The cells do not change their colors and function as expected. So I am pretty sure that the problem lies with the CAGradientLayer.

I have tried everything that I could think of but nothing seems to work!

Answer

Try this once,

 [cell.layer insertSublayer:gradient atIndex:[[cell.layer sublayers] indexOfObject:[[cell.layer sublayers] lastObject]]];

instead of

 [cell.layer insertSublayer:gradient atIndex:0];
Comments