如何在表格视图单元格上设置突出显示的图像?
问题描述:
我已经按照苹果的AdvancedTableViewCells的代码,并建立与细胞背景图像的表视图。现在我想改变它,使其不是蓝色突出显示颜色,而是显示我的图像的较暗版本。我需要遵循的步骤是什么?我正在使用带有自定义NIB的UITableViewCell
子类。我的背景图像是作为cell.backgroundView
实施的。如何在表格视图单元格上设置突出显示的图像?
我拿到目前为止的步骤是:
- 改变细胞的
selectionStyle
为“无” - 设置我的
UILabel
子视图中突出显示的颜色浅色 - 创建我的背景较暗的版本作为单独的图像
- 覆盖
setSelected: animated:
我想知道接下来的步骤。
答
您是否尝试过更换内部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
}
}
答
我发现了selectedBackgroundView
属性。我使用这种方法,而不是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;
}
我不知道这是否是正确的方法,因为它引入了一些其他问题。有一件事是单元selectionStyle
必须是UITableViewCellSelectionStyleNone
以外的东西,否则它不会显示背景图像。取消动画也停止了。我会开一个关于这些问题的新问题。
感谢您的回答。这是我正在尝试的,但我必须手动处理取消选择。 – 2010-03-12 02:33:42