-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [cell setBackgroundColor:[UIColor clearColor]]; CAGradientLayer *grad = [CAGradientLayer layer]; grad.frame = cell.bounds; grad.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:52.0f/255.0f green:50.0f/255.0f blue:51.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:35.0f/255.0f green:34.0f/255.0f blue:34.0f/255.0f alpha:1.0f] CGColor], nil]; [cell setBackgroundView:[[UIView alloc] init]]; [cell.backgroundView.layer insertSublayer:grad atIndex:0]; CAGradientLayer *selectedGrad = [CAGradientLayer layer]; selectedGrad.frame = cell.bounds; selectedGrad.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; [cell setSelectedBackgroundView:[[UIView alloc] init]]; [cell.selectedBackgroundView.layer insertSublayer:selectedGrad atIndex:0]; }
转自https://coderwall.com/p/tolkaa/uitableview-cells-background-gradient-ios-objective-c