【问题标题】:How to change the blue highlight color of a UITableViewCell?如何更改 UITableViewCell 的蓝色突出显示颜色?
【发布时间】:2011-02-02 23:15:12
【问题描述】:

我想知道如何更改 UITableViewCell 的蓝色突出显示/选择颜色,有什么想法吗?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    您可以通过多种方式更改突出显示颜色。

    1. 更改单元格的 selectionStyle 属性。如果你把它改成UITableViewCellSelectionStyleGray,它会是灰色的。

    2. 更改selectedBackgroundView 属性。实际上,创建蓝色渐变的是视图。您可以创建一个视图并绘制您喜欢的任何内容,并将该视图用作表格视图单元格的背景。

    【讨论】:

    • 酷,在这里找到了很好的解释和示例:cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
    • 我意识到在 IB 中你不能将 UITableViewCell“Selection”设置为“None”——因为 selectedBackgroundView 将永远不会出现。只有将“选择”设置为默认值后,selectedBackgroundView 才会出现。在 iOS 6 和 7 上测试。
    • 感谢您的好评!现在变得如此简单:您创建一个新视图(甚至无需指定框架:cell.selectedBackgroundView = [UIView new]; 并设置您想要的任何颜色:cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHex:@"ecf2f5" andAlpha:1];
    • 看这个页面底部,还有更多最近的方法
    【解决方案2】:

    Zonble 已经提供了一个很好的答案。 我认为包含一个短代码 sn-p 用于将 UIView 添加到将显示为选定背景视图的表格视图单元格中可能很有用。

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    
        UIView *selectionColor = [[UIView alloc] init];
        selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
        cell.selectedBackgroundView = selectionColor;
    
    • 单元格是我的UITableViewCell
    • 我创建了一个 UIView 并使用 RGB 颜色(浅灰色)设置它的背景色
    • 然后我将单元格 selectedBackgroundView 设置为使用我选择的背景颜色创建的 UIView

    这对我来说效果很好。 感谢 Zonble 的提示。

    【讨论】:

    • 这在使用带有部分的表视图时效果不佳。选中后,单元格将失去边框,并且在第一个或最后一个单元格时不圆角。
    • @kirk,你的意思是分组:)
    • @Tieme,不,我的意思是sections:“表格视图由零个或多个部分组成,每个部分都有自己的行。”
    • 你必须把它放在'cellForRowAtIndexPath'中。对我来说效果很好,只有一节。没有尝试过多个部分。
    • 游戏有点晚了,但我可以确认它在具有多个部分(未分组)的 tableview 中工作
    【解决方案3】:

    UITableViewCell 具有三种默认选择样式:-

    1. 蓝色
    2. 灰色

    实现如下:-

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
    
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];       
    }
    

    【讨论】:

    • 除了这三种默认样式,我们还可以通过 selectedBackgroundView 属性自定义我们期望的样式。
    • 为我工作...非常感谢。
    【解决方案4】:

    在 Swift 中,在 cellForRowAtIndexPath 中使用它

    let selectedView = UIView()
    selectedView.backgroundColor = .white
    cell.selectedBackgroundView = selectedView
    

    如果您希望您的选择颜色在每个UITableViewCell 中都相同, 在AppDelegate 中使用它。

    let selectedView = UIView()
    selectedView.backgroundColor = .white
    UITableViewCell.appearance().selectedBackgroundView = selectedView
    

    【讨论】:

      【解决方案5】:

      如果您想在应用范围内更改它,您可以将逻辑添加到您的 App Delegate

      class AppDelegate: UIResponder, UIApplicationDelegate {
      
          //... truncated
      
         func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
      
              // set up your background color view
              let colorView = UIView()
              colorView.backgroundColor = UIColor.yellowColor()
      
              // use UITableViewCell.appearance() to configure 
              // the default appearance of all UITableViewCells in your app
              UITableViewCell.appearance().selectedBackgroundView = colorView
      
              return true
          }
      
          //... truncated
      }
      

      【讨论】:

      • 完美。知道如何在选择时全局设置默认单元格的文本颜色吗?
      • 这行得通,但是当推送新的视图控制器时,选择会被直接清除。
      • 对 UITableViewCell 进行全局更改的好方法
      【解决方案6】:

      为了完整性:如果您创建了自己的UITableViewCell 子类,则可以实现- (void)setSelected:(BOOL)selected animated:(BOOL)animated 方法,并设置您在内容视图中添加的某些视图的背景颜色。 (如果是这种情况)或 contentView 本身(如果它没有被您自己的视图之一覆盖。

      - (void)setSelected:(BOOL)selected animated:(BOOL)animated
      {
          if(selected) {
              self.contentView.backgroundColor = UIColor.blueColor;
          } else {
              self.contentView.backgroundColor = UIColor.whiteColor;
          }
      }
      

      (没有使用?来适应源代码DIV的小宽度:)

      与使用 selectedBackgroundView 相比,这种方法有两个优点,它使用更少的内存和稍微更少的 CPU,除非你显示数百个单元格,否则你甚至不会注意到。

      【讨论】:

      • 这仅在将新视图分配给selectedBackgroundView 后才有效在方法的最开始添加self. selectedBackgroundView = [UIView new];
      • @enadun 这种方法在 iOS 12 上适用于我,根本没有设置 selectedBackgroundView 属性。
      【解决方案7】:

      我必须将选择样式设置为UITableViewCellSelectionStyleDefault 才能使用自定义背景颜色。如果有任何其他样式,自定义背景颜色将被忽略。在 iOS 8 上测试。

      单元格的完整代码如下:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          static NSString *CellIdentifier = @"MyCell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }
      
          // This is how you change the background color
          cell.selectionStyle = UITableViewCellSelectionStyleDefault;
          UIView *bgColorView = [[UIView alloc] init];
          bgColorView.backgroundColor = [UIColor redColor];
          [cell setSelectedBackgroundView:bgColorView];
      
          return cell;
      }
      

      【讨论】:

        【解决方案8】:
        @IBDesignable class UIDesignableTableViewCell: UITableViewCell {
          @IBInspectable var selectedColor: UIColor = UIColor.clearColor() {
            didSet {
              selectedBackgroundView = UIView()
              selectedBackgroundView?.backgroundColor = selectedColor
            }
          }
        }
        

        在故事板中,将 UITableViewCell 的类设置为 UIDesignableTableViewCell,在属性检查器上,您可以将单元格的选定颜色更改为任何颜色。

        您可以将其用于所有单元格。 这就是您的属性检查器的外观。

        【讨论】:

        • 很有创意!太棒了!
        • 你可以让它更简单,看看我的回答;)stackoverflow.com/a/51764804/537694
        • 我使用自定义单元格来设置标签,所以效果很好,谢谢! Swift 5 的变化:现在是 UIColor.clear,而不是 UIColor.clearColor()。不确定这是否也是 Swift 的一个变化,但它也可以在没有 @IBDesignable 的情况下工作。
        【解决方案9】:

        基于@user's answer,您可以在应用代码的任何位置添加此扩展,并直接在故事板编辑器中为应用的每个单元格设置选择颜色:

        @IBDesignable extension UITableViewCell {
            @IBInspectable var selectedColor: UIColor? {
                set {
                    if let color = newValue {
                        selectedBackgroundView = UIView()
                        selectedBackgroundView!.backgroundColor = color
                    } else {
                        selectedBackgroundView = nil
                    }
                }
                get {
                    return selectedBackgroundView?.backgroundColor
                }
            }
        }
        

        【讨论】:

          【解决方案10】:

          Swift 3.0/4.0

          如果您创建了自己的自定义单元格,则可以更改 awakeFromNib() 上所有单元格的选择颜色:

           override func awakeFromNib() {
              super.awakeFromNib()
          
              let colorView = UIView()
              colorView.backgroundColor = UIColor.orange.withAlphaComponent(0.4)
          
              self.selectedBackgroundView = colorView
          
          
          }
          

          【讨论】:

            【解决方案11】:

            1- 将视图添加到单元格的内容视图中。
            2- 右键单击​​您的单元格。
            3-将添加的视图设为“selectedBackgroundView”

            【讨论】:

            • 您可以将新的UIView 添加到UI(Table)ViewController,而无需将其添加到视图层次结构中,只需将其拖动到界面设计器中控制器上方的栏即可。然后你可以按照上面的技巧将其设置为单元格的selectedBackgroundView
            【解决方案12】:

            斯威夫特 5

            另一种解决方案是仅覆盖 UITableViewCell 上的 set selected 方法。

            override func setSelected(_ selected: Bool, animated: Bool) {
                super.setSelected(selected, animated: animated)
            
                // Configure the view for the selected state
                let selectionView = UIView()
                selectionView.backgroundColor = UIColor.green
                // selectionView.alpha = 0.3 // optional if you want to tone down a colour
                self.selectedBackgroundView = selectionView
            }
            

            注意:适用于 Mac Catalyst。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-05-17
              • 2013-04-17
              • 2010-12-12
              • 1970-01-01
              • 1970-01-01
              • 2012-10-24
              • 1970-01-01
              • 2012-03-27
              相关资源
              最近更新 更多