0

I am applying a gradient in my custom UITableViewCell using Storyboards inside my cellForRowAtIndexPath method and I wanted to prevent my application of CAGradientLayer gradient to overlap. How can I do this?

This is my current code I'm using to prevent it from overlapping but the gradient only applies to the latest indexPath loaded in UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];

    if (!gradientLayer) {
        gradientLayer = [CAGradientLayer layer];
    }
    gradientLayer.frame = cell.backgroundImage.bounds;
    gradientLayer.colors =  @[(id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor];
    [cell.backgroundImage.layer insertSublayer:gradientLayer atIndex:0];

    return cell;
}

My cell.backgroundImage is simply an UIImageView where I load an image and apply a gradient in it.

jaytrixz
  • 4,059
  • 7
  • 38
  • 57

1 Answers1

0

I found the answer that's working. I just set my backgroundImage sublayers to nil before applying the gradient.

cell.backgroundImage.layer.sublayers = nil;
jaytrixz
  • 4,059
  • 7
  • 38
  • 57