I have the following code that creates a gradient background for a UITableViewCell. The gradient came out great. However when I try selecting a row, I don’t see the regular blue highlighted row. If I remove the custom gradient code, the selected row highlights work fine. I am not sure what I am missing here. Any help is greatly appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TimeTableViewCellList";
TimeTableViewCell *cell = (TimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"TimeCell" owner:self options:nil] lastObject];
//To create the cell gradient
UIColor *startColor = [UIColor whiteColor];
UIColor *endColor = [UIColor colorWithRed:247.0/255.0 green:243.0/255.0 blue:238.0/255.0 alpha:1.0];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
}
TimeEntry * entry = [[self getTimeEntriesBySection:indexPath.section] objectAtIndex:indexPath.row];
//..OTHER CELL VALUES SET HERE
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}
Your gradient layer will be obscuring the selected effect.
Try adding the gradient layer to
cell.contentView.layerinstead. If that doesn’t work, you may have to make the layer a property of your cell subclass and change its visibility in your override ofsetSelected:animated: