【问题标题】:selection color for a UITableViewCellUITableViewCell 的选择颜色
【发布时间】:2023-04-02 09:00:01
【问题描述】:

如果我有一个自定义 UITableViewCell 不使用单元格中内置的 textLabel 而是使用它自己的绘图,我如何在选择时更改 contentView 的外观,就像它自动一样对于默认文本(可通过设置selectedTextColor: 自定义)?

如果我更改tableView:willSelectRowAtIndexPath:,那么它只会在蓝色选择背景启动后更新,而不是在它像我想要的那样动画时更新。

【问题讨论】:

    标签: iphone cocoa-touch iphone-sdk-3.0 uitableview


    【解决方案1】:

    只是不要继承 UITableViewCell 并使用默认行为。 您可以完全自定义单元格而无需任何子类化。

    阅读this article了解更多详情。

    【讨论】:

    • 这实际上就是我在做的事情,但是您的链接仍然为我指明了正确的方向。我需要更改 UILabel 的 highlightTextColor。
    • 当您谈到“自己的绘图”时,我立即想到了子类化。我的错。很高兴您找到解决方案
    【解决方案2】:

    在您的 tableview cellForRowAtIndexPath 方法中添加此代码,然后更改 UITableViewCell 选择样式的预期颜色。

       //-------------------------------------------------------------------------
       //background selected view 
       UIView *viwSelectedBackgroundView=[[UIView alloc]init];
       viwSelectedBackgroundView.backgroundColor=[UIColor colorWithRed:124.0/255.0 green:202.0/255.0 blue:227.0/255.0 alpha:1.0];
       cell.selectedBackgroundView=viwSelectedBackgroundView;
       //-------------------------------------------------------------------------
    

    【讨论】:

      【解决方案3】:

      如果您已将 UITableViewCell 子类化,则可以通过覆盖以下内容来自定义单元格的元素:

      - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
          if(highlighted) {
              self.backgroundColor = [UIColor redColor];
          } else {
              self.backgroundColor = [UIColor clearColor];
          }
      
          [super setHighlighted:highlighted animated:animated];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多