如何更改 UITableViewCell 的蓝色突出显示颜色?

我想知道如何改变蓝色高光/选择颜色的 UITableViewCell,有什么想法吗?

127397 次浏览

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

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

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

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

  1. 蓝色
  2. 格雷
  3. 没有

执行情况如下:

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


[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

Zonble 已经提供了一个很好的答案。 我认为包含一个简短的代码片段可能会有用,这个代码片段用于将 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;
  • 手机是我的 UITableViewCell
  • 我创建了一个 UIView,并设置其背景颜色使用 RGB 颜色(浅灰色)
  • 然后,我设置细胞 selectedBackgroundView为我创建与我选择的背景颜色的 UIView

这招对我很管用。 谢谢你的提示 Zonble。

如果您想在应用程序范围内更改它,可以将逻辑添加到应用程序委托

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
}

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

该单元的完整代码如下:

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

为了完整起见: 如果您创建了自己的 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 的小宽度:)

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

在 Swift 中,在 cellForRowAtIndexPath中使用这个

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

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

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

在情节串连图板中,将 UITableViewCell 的类设置为 UIDesignableTableViewCell,在 Attribute 检查器上,可以将单元格的选定颜色更改为任意颜色。

你可以把它用在你所有的细胞上。 这就是你的属性检查员的样子。

This is how your Attributes inspector will look like

Swift 3.0/4.0

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

 override func awakeFromNib() {
super.awakeFromNib()


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


self.selectedBackgroundView = colorView




}

基于 @ 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

将视图添加到单元格的内容视图中。
右键单击您的单元格。
3-将添加的视图设置为“ selectedBackoundView” enter image description here

Swift 5

另一种解决方案是只覆盖 UITableViewCell上的 set 选择方法。

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 中工作。