Gradient backgrounds work in iOS 8 but not iOS 7

So I'm building a table view centric app, and I'm trying to include a gradient background for each table view cell, using the following code:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.94 green:0.84 blue:0.49 alpha:1.0]CGColor], (id)[[UIColor colorWithRed:0.93 green:0.86 blue:0.61 alpha:1.0]CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:0.25 green:0.51 blue:0.00 alpha:1.0];

Now, in the simulator that I am using, which is running iOS 8 (using Xcode 6 Beta 2), I get this result:

But on my device (sorry for the Assistive touch, my lock button is broken...), running iOS 7, it looks like this:

My gradients throughout the app in the main view background work on both, it's just the gradients in the table cell views that don't work. As you can also see in these screenshots, the section header views also have gradients, but these work in both versions. Is there a certain flag I need to be looking for to make this work in iOS 7?

Edit: This is being called in cellForRowAtIndexPath.

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

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.94 green:0.84 blue:0.49 alpha:1.0]CGColor], (id)[[UIColor colorWithRed:0.93 green:0.86 blue:0.61 alpha:1.0]CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:0.25 green:0.51 blue:0.00 alpha:1.0];

NSDictionary *match;
if(indexPath.section == 0) {
    match = [_inprogressMatches objectAtIndex:indexPath.row];
} else if(indexPath.section == 1) {
    match = [_finalMatches objectAtIndex:indexPath.row];
} else {
    match = [_pregameMatches objectAtIndex:indexPath.row];
}

NSDictionary *homeTeam = [match objectForKey:@"homeTeamId"];
NSDictionary *awayTeam = [match objectForKey:@"awayTeamId"];    
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ : %@ %@",[awayTeam objectForKey:@"name"] , [match objectForKey:@"awayScore"], [match objectForKey:@"homeScore"], [homeTeam objectForKey:@"name"]];

return cell;
}

Answers


To put a gradient in the background of a cell, don't mess with its layer (which you are doing wrong in any case, since you are not checking for the existence of the layer in a reused cell).

Give the cell a backgroundView whose layer is the gradient or has the gradient as sublayer. That is what backgroundView is for.


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.