ios – gradient maintains the change between the cells in the collection view quickly

I have a gradient background for the cell in my collection view and each gradient is different depending on which label the cell is in (for example, Christmas is a purple gradient, birthday is a green gradient), however, when scrolling by the collection view, the gradients continue to change the cells. Is there a way to fix this happening?

This is the code I use to set the gradient in the background view. Is within cellForItemAt

cell.mainView.setGradientBackground (colors: self.getColourFromTag (tag: self.lists[indexPath.item].label))

.setGradientBackground is an extension of UIView that only sets a gradient in the background. I use the following method to obtain the colors of the gradient.

func getColourFromTag (tag: String) -> [CGColor] {

if the label == "christmas" {

he came back [Colours.gradients.festive.start.cgColor, Colours.gradients.festive.end.cgColor]

    } else if tag == "Birthday" {

he came back [Colours.gradients.lime.start.cgColor, Colours.gradients.lime.end.cgColor]

    } else if tag == "Valentines Day" {

he came back [Colours.gradients.strawberry.start.cgColor, Colours.gradients.strawberry.end.cgColor]

    } else if tag == "Charity" {

he came back [Colours.gradients.blueberry.start.cgColor, Colours.gradients.blueberry.end.cgColor]

    } else if tag == "Event" {

he came back [,]

he came back [Colours.gradients.midnight.start.cgColor, Colours.gradients.midnight.end.cgColor]

I have tried adding each one [colour] in a matrix, then placing that in the .setGradientBackground at indexPath.item like:

var colors =


colors[indexPath.item] = self.getColourFromTag (tag: self.lists[indexPath.item].label)
cell.mainView.setGradientBackground (colors: colors[indexPath.item])

However, this does not work since it is out of range. Does anyone have a solution? Thank you.