ios 如何更改 UITableViewCell 的蓝色高亮颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2553746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:05:46  来源:igfitidea点击:

How to change the blue highlight color of a UITableViewCell?

iosuitableview

提问by Thomas Joos

I'm wondering how to change the blue highlight/selection color of a UITableViewCell, any ideas?

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

回答by zonble

You can change the highlight color in several ways.

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

  1. Change the selectionStyle property of your cell. If you change it to UITableViewCellSelectionStyleGray, it will be gray.

  2. Change the selectedBackgroundViewproperty. Actually what creates the blue gradient is a view. You can create a view and draw what ever you like, and use the view as the background of your table view cells.

  1. 更改单元格的 selectionStyle 属性。如果将其更改为UITableViewCellSelectionStyleGray,它将是灰色的。

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

回答by T Davey

Zonble has already provided an excellent answer. I thought it may be useful to include a short code snippet for adding a UIViewto the tableview cell that will present as the selected background view.

Zonble 已经提供了一个很好的答案。我认为包含一个简短的代码片段可能很有用,用于将 a 添加UIView到将作为选定背景视图显示的 tableview 单元格。

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;
  • cell is my UITableViewCell
  • I created a UIView and set its background color using RGB colours (light gray)
  • I then set the cell selectedBackgroundViewto be the UIViewthat I created with my chosen background colour
  • 细胞是我的 UITableViewCell
  • 我创建了一个 UIView 并使用 RGB 颜色(浅灰色)设置其背景颜色
  • 然后我设置单元格selectedBackgroundViewUIView我创造了我的选择背景颜色

This worked well for me. Thanks for the tip Zonble.

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

回答by Arshad Parwez

UITableViewCellhas three default selection styles:-

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

  1. Blue
  2. Gray
  3. None
  1. 蓝色
  2. 灰色的
  3. 没有任何

Implementation is as follows:-

实施如下:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {

     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];       
}

回答by alitosuner

In Swift, use this in cellForRowAtIndexPath

在 Swift 中,在 cellForRowAtIndexPath

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

If you want your selection color be the same in every UITableViewCell, use this in AppDelegate.

如果您希望您的选择颜色在每个中都相同,请UITableViewCellAppDelegate.

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

回答by NatashaTheRobot

If you want to change it app wide, you can add the logic to your App Delegate

如果你想在应用范围内改变它,你可以将逻辑添加到你的 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
}

回答by Martijn Scheffer

for completeness: if you created your own subclass of UITableViewCellyou can implement the - (void)setSelected:(BOOL)selected animated:(BOOL)animatedmethod, and set the background color of some view you added in the content view. (if that is the case) or of the contentView itself (if it is not covered by one of your own views.

为了完整性:如果您创建了自己的子类,则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;
    }
}

(did not use ? to fit the small width of source code DIV's :)

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

this approach has two advantages over using selectedBackgroundView, it uses less memory, and slightly less CPU, not that u would even notice unless u display hundreds of cells.

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

回答by samwize

I have to set the selection style to UITableViewCellSelectionStyleDefaultfor custom background color to work. If any other style, the custom background color will be ignored. Tested on iOS 8.

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

The full code for the cell as follows:

单元格的完整代码如下:

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

回答by user

@IBDesignable class UIDesignableTableViewCell: UITableViewCell {
  @IBInspectable var selectedColor: UIColor = UIColor.clearColor() {
    didSet {
      selectedBackgroundView = UIView()
      selectedBackgroundView?.backgroundColor = selectedColor
    }
  }
}

In your storyboard, set the class of your UITableViewCell to UIDesignableTableViewCell, on the Attributes inspector, you may change the selected color of your cell to any color.

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

You can use this for all of your cells. This is how your Attributes inspector will look like.

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

This is how your Attributes inspector will look like

这就是你的属性检查器的样子

回答by Sam Bing

Swift 3.0/4.0

斯威夫特 3.0/4.0

If you have created your own custom cell you can change the selection color on awakeFromNib()for all of the cells:

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

 override func awakeFromNib() {
    super.awakeFromNib()

    let colorView = UIView()
    colorView.backgroundColor = UIColor.orange.withAlphaComponent(0.4)

    self.selectedBackgroundView = colorView


}

回答by Climbatize

Based on @user's answer, you can just add this extension anywhere in your app code and have your selection color directly in storyboard editor for every cells of your app :

根据@user 的回答,您可以在应用程序代码的任何位置添加此扩展程序,并直接在故事板编辑器中为应用程序的每个单元格选择颜色:

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

UITableViewCell selection color in storyboard

故事板中的 UITableViewCell 选择颜色