将 UIImageView 从正常更改为突出显示不起作用

Posted

技术标签:

【中文标题】将 UIImageView 从正常更改为突出显示不起作用【英文标题】:Changing UIImageView from normal to highlighted doesn't work 【发布时间】:2012-09-25 03:30:06 【问题描述】:

我有一个 UIImageView 作为我的 UITableViewCell 子类的背景。我已将图像放在图像字段中,并将该图像的较轻版本放在突出显示的字段中。在我的 UITableViewCell 子类中,我添加了将 ImageView 设置为突出显示的代码,但没有任何变化......

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
    self.backgroundImageView.highlighted = selected;
    NSLog(@"selected: %d", selected);

我已验证 self.backgroundImageView 已正确连接为 IBOutlet。

【问题讨论】:

【参考方案1】:

只需像这样初始化 UIImageView:

[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gray_arrow"] highlightedImage:[UIImage imageNamed:@"white_arrow"]];

如果您需要保持它突出显示,在您的 tableview 的 didSelectRowAtIndexPath 方法中,获取代表您的 UIImageView 的子视图并使其突出显示。像这样的:

UIImageView* imageView = [[[dataSource objectAtIndex:[indexPath row]] subviews] objectAtIndex:2]
[imageView setHighlighted:YES];

将其他单元格中的 UIImageView 设置为:

 [imageView setHighlighted:NO];

【讨论】:

以上是关于将 UIImageView 从正常更改为突出显示不起作用的主要内容,如果未能解决你的问题,请参考以下文章