[英]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. 我遵循了Apple AdvancedTableViewCells中的代码,并使用单元格的背景图像构建了表格视图。 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. 我正在使用带有自定义NIB的
UITableViewCell
子类。 My background image is implemented as the cell.backgroundView
. 我的背景图像实现为
cell.backgroundView
。
The steps I've take so far are: 到目前为止,我采取的步骤是:
selectionStyle
of the cell to "None" selectionStyle
更改为“ None” UILabel
subviews to a light colour UILabel
子视图上的突出显示颜色设置为浅色 setSelected: animated:
setSelected: animated:
I'm wondering about the next steps. 我想知道下一步。
Have you tried to replace the image inside 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
}
}
I found the selectedBackgroundView
property. 我找到了
selectedBackgroundView
属性。 I'm using this approach instead of setSelected: animated:
我正在使用这种方法,而不是
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. 一件事是单元格
selectionStyle
必须是UITableViewCellSelectionStyleNone
以外的其他东西,否则它不会显示背景图像。 The deselection animation has stopped working too. 取消选择动画也已停止工作。 I'll open a new question about these problems.
我将针对这些问题提出一个新问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.