问题
I want to style a UITableView
to look a lot like if it had the style UITableViewStyleGrouped
but with some subtle differences (like different colors and rounded corners with a shorter radius). The rows have variable height. I want to use this custom style for most table view controllers in my app. See the Twitter, Groupon, & GitHub Issues apps for examples.
How should I do this?
I'm thinking of doing like UITableViewStyleGrouped
does. UITableViewStyleGrouped
sets backgroundView
& selectedBackgroundView
both to instances of UIGroupTableViewCellBackground
(a subclass of UIView
). UIGroupTableViewCellBackground
is a delegate of its layer and implements drawLayer:
to set its layer's contents to a CGImageRef
.
I'm pretty sure Apple creates this
CGImageRef
according to Quartz 2D Programming Guide : Creating a Bitmap Graphics Context, which also suggests considering usingCGLayer
instead of drawing to a bitmap graphics context. Which is better for this application?Also, the first cell of a
UITableViewStyleGrouped
table view adds a shadow with rounded-corners to its top usingUIImageView
(It sets it to a resizable (width-wise)UIImage
with a translucent PNG file.) Why does it do that? Doesn't this slow scrolling? Why doesn't it just draw this to theCGImageRef
for the first cell? Maybe the decrease in performance wasn't significant and it was easier to get the cell to look correct with an image. I saved the cell'sCGImageRef
to disk and opened it with Preview. It still has rounded corners. This overlaid view just adds the top shadow.Below is a screenshot of running the Core Graphics Instruments tool on my device with "Color Blended Layers" selected for a
UITableViewStyleGrouped
table view. You can see that the shadow at the top is blended. And, it looks like there's some blending going on in the last cell too.With the third cell selected (no blending). Selecting the first or last cell acts the same but doesn't get rid of the already blended overlaid views.
回答1:
you can count the number of rows being drawn and set the image as background for individual cell.
What you have to do is
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
use this method to set the background image
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
&&&&
if((indexPath.row)==0)
// set image upper corner image here
else if (indexPath.row==1)
//set normal image here
else if (indexPath.row==YourArray)
// also set the lower corner round image in the last low, which can be calculated by comparing indexpath.row to the mutablearray/array
}
You can also apply one condition in before the &&& part to apply a different image which has both upper and lower corners round in case array.count == 1
来源:https://stackoverflow.com/questions/9314860/custom-uitableviewcell-backgroundview-selectedbackgroundview