删除高亮颜色,但在Custom UITableViewCell中保持图像高亮显示

时间:2010-09-20 01:25:04

标签: iphone image uitableview selection highlight

我正在搜索这段时间,但我从未找到解决方案。

我有一个带有自定义单元格的UITableView,这些单元格有一个图像,我将标签放在上面。

问题是,当我选择一个单元格时,使用默认的高亮颜色选择整个单元格框架。我尝试更改样式,但如果我使用cell.selectionStyle = UITableViewCellSelectionStyleNone;,图像将不会突出显示...

以防万一有人要求我创建区分高亮/正常状态的图像。

6 个答案:

答案 0 :(得分:1)

在UITableViewCell中有一个名为highlight的属性。

@property(nonatomic, getter=isHighlighted) BOOL highlighted

因此,您可以通过覆盖此方法来修改突出显示的行为。

- (void)setHighlighted:(BOOL)value
{
   [super setHighlighted:value];

   // Do custom highlighted behavior here
   ...
}

或覆盖setHighlighted:animated:方法。


Something like this?

In MyCustomCell.m

- (void)setHighlighted:(BOOL)value
{
   [super setHighlighted:value];

   // Do custom highlighted behavior here
   if (value == YES)
   {
        // show highlighted image 
   } else {
        // show image for normal state. 
   }

   // propagate the highlighted message to other views.
   [mChildView setHighlighted:value];
}

答案 1 :(得分:1)

以下是我的工作方式:

我正在为我的手机使用XIB。在那里,我有一个背景图像(没有连接到任何插座),这是我的正常状态背景。我将该图像设置为也有高亮显示的图像(在我的情况下为蓝色版本)。

我添加了另一个UIView,将其背景颜色设置为清除,并将其附加到单元格的selectedBackgroundView插座。这样做可以防止正常的内置“设置整个背景蓝色”行为。

然后我将单元格的选择样式设置为蓝色(只是不能为None,因为不会发生突出显示)。

答案 2 :(得分:0)

我找到了一个非常好的解决方案。

我没有必要覆盖任何方法。

1 - 在我的CustomCell.xib中,我创建了2个imageViews。一个我连接到myImageView插座,另一个连接到selectedBackgroundView。

2 - 对于正常状态我放了myCustomBackgroundImg.png并且在突出显示的图像中我放了一个空的png,因此当它被选中时它不会停留在backgroundView的顶部。

3 - 在selectedBackgroundView中,我将myCustomSelectedBackgroundImg.png置于正常状态图像中。

希望这有助于其他人。

答案 3 :(得分:0)

改进了Toro的代码。

有效!

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    if (highlighted == YES) {
        // show highlighted image
        [UIView animateWithDuration:0.1f animations:^{
            self.imageViewBanner.alpha = 0.3;
        }];
    } else {
        // show image for normal state.
        [UIView animateWithDuration:0.1f animations:^{
            self.imageViewBanner.alpha = 1.0;
        }];
    }

    [super setHighlighted:highlighted animated:animated];
}

答案 4 :(得分:0)

我能找到的最佳解决方案是用透明视图替换单元格的selectedBackgroundView,并将selectionStyle留给UITableViewCellSelectionStyleBlue,以便突出显示的状态仍然传播到所有子视图(使用UITableViewCellSelectionStyleNone停止突出显示传播到子视图)。

UIView *transparentBackground = [[UIView alloc] initWithFrame:cell.bounds];
transparentBackground.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
transparentBackground.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = transparentBackground;

答案 5 :(得分:0)

100%工作且简单的解决方案:

步骤1:在代码或InterfaceBuilder中设置cell.selectionStyle = UITableViewCellSelectionStyleNone; 第2步:添加2个方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.highlighted = YES;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.highlighted = NO;
}
Swift中的

cell?.selectionStyle = .None


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.imageView?.highlighted = true;
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.imageView?.highlighted = false;
}