简体   繁体   English

如何在表格视图单元格上设置突出显示的图像?

[英]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: 到目前为止,我采取的步骤是:

  • Change the selectionStyle of the cell to "None" 将单元格的selectionStyle更改为“ None”
  • Set the Highlight colour on my UILabel subviews to a light colour 将我的UILabel子视图上的突出显示颜色设置为浅色
  • Create a darker version of my background as a separate image 将背景的深色版本创建为单独的图像
  • Override 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM