How can I set a highlighted image on a table view cell?
I've followed the code in Apple's AdvancedTableViewCells and built a table view with a background image for cells. Now I want to change it so that instead of the blue highlight colour, it shows a darker version of my image. What are the steps I need to follow to do this? I'm using a UITableViewCell
subclass with a custom NIB. My background image is implemented as the cell.backgroundView
.
The steps I've take so far are:
- Change the
selectionStyle
of the cell to "None" - Set the Highlight colour on my
UILabel
subviews to a light colour - Create a darker version of my background as a separate image
- Override
setSelected: ani开发者_Python百科mated:
I'm wondering about the next steps.
Have you tried to replace the image inside setSelected: animated:
?
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
// replace image here
// or move a pointer from one image to another
}
}
I found the selectedBackgroundView
property. I'm using this approach instead of setSelected: animated:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:@"TableBackground" ofType:@"png"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundImagePath];
self.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
self.backgroundView.frame = self.bounds;
NSString *selectedBackgroundImagePath = [[NSBundle mainBundle] pathForResource:@"TableBackgroundDark" ofType:@"png"];
UIImage *selectedBackgroundImage = [UIImage imageWithContentsOfFile:selectedBackgroundImagePath];
self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:selectedBackgroundImage] autorelease];
self.selectedBackgroundView.frame = self.bounds;
return self;
}
I'm not sure if this is the correct way, as it's introduced a couple of other problems. One thing is that the cell selectionStyle
has to be something other than UITableViewCellSelectionStyleNone
or it won't show the background image. The deselection animation has stopped working too. I'll open a new question about these problems.
精彩评论