How can i make the cells in my UITableViewController have a gradient?
I'm trying to figure out how to make the cells in my UITableViewController class have a gradient like the ones in this question (http://stackoverflow.com/questions/6880416/quick-question-with-uitabl开发者_JAVA百科eviewcell-shadow). i've seen several things saying to use an image background, but i'd rather do it with code. i'm having trouble finding out how to do this and appreciate any help.
Matt Gallagher did a nice walkthrough of this a while back: Adding shadow effects to UITableView using CAGradientLayer.
It uses a custom UIView subclass that draws a CAGradientLayer as the background view of the cell. The code to set up the view is trivial—
In the GradientView UIView subclass:
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor,
(id)[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0].CGColor, nil];
self.backgroundColor = [UIColor clearColor];
In the UITableViewController's cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [{ Set up your cell}];
cell.backgroundView = [[[GradientView alloc] init] autorelease];
}
Naturally, there are a few complications, but there's a nice, straightforward sample app on Matt's site.
Just a nit: the cells are in the UITableView
, not the controller.
精彩评论