【问题标题】:Remove highlight color but maintain image highlight in Custom UITableViewCell删除突出显示颜色但在自定义 UITableViewCell 中保持图像突出显示
【发布时间】:2011-04-14 11:22:16
【问题描述】:

我已经搜索了一段时间,但我从未找到解决方案。

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

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

以防万一有人问我为突出显示/正常状态创建了不同的图像。

【问题讨论】:

    标签: iphone image uitableview selection highlight


    【解决方案1】:

    100% 有效且简单的解决方案:

    第 1 步:在代码或 InterfaceBuilder 中设置 cell.selectionStyle = UITableViewCellSelectionStyleNone;
    第二步:添加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;
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        改进了 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】:

          我是这样工作的:

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

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

          然后我将单元格的选择样式设置为蓝色(不能为无,因为此时不会突出显示)。

          【讨论】:

            【解决方案5】:

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

            我不必重写任何方法。

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

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

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

            希望这对其他人有所帮助。

            【讨论】:

              【解决方案6】:

              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];
              }
              

              【讨论】:

              • 你能给我一些关于如何做到这一点的代码吗?将图像置于高亮状态但不高亮单元格
              猜你喜欢
              • 2011-10-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-30
              • 1970-01-01
              相关资源
              最近更新 更多