I have a custom UITableViewCell with a custom layout. I wanted a gradient background, so in my UITableViewDelegate cellForRowAtIndexPath: method, I create a CAGradientLayer and add it to the cell's layer with insertSubLayer:atIndex: (using index 0). This works just fine except for two things:
Most importantly, I can't figure out how to change to a different gradient color when the row is highlighted. I have tried a couple things, but I'm just not familiar enough with the framework to get it working. Where would be the ideal place to put that code, inside the table delegate or the cell itself?
Also, there's a 1px white space in between each cell in the table. I have a background color on the main view, a background color on the table, and a background color on the cell. Is there some kind of padding or spacer by default in a UITableView?
I know this thread is old, but here's a solution for the first part of your question (adding a gradient to the selected and non-selected states of a cell):
-(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 whiteColor] CGColor], (id)[[UIColor blackColor] 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];
}
I'm not sure about the first question but I think you can set the selectedBackgroundView property similarly to how you set the backgroundView property. The white space between cells is probably the separator. You can change that color like
tableView.separatorColor = [UIColor redColor];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With