【发布时间】:2011-02-02 23:15:12
【问题描述】:
我想知道如何更改 UITableViewCell 的蓝色突出显示/选择颜色,有什么想法吗?
【问题讨论】:
标签: ios uitableview
我想知道如何更改 UITableViewCell 的蓝色突出显示/选择颜色,有什么想法吗?
【问题讨论】:
标签: ios uitableview
您可以通过多种方式更改突出显示颜色。
更改单元格的 selectionStyle 属性。如果你把它改成UITableViewCellSelectionStyleGray,它会是灰色的。
更改selectedBackgroundView 属性。实际上,创建蓝色渐变的是视图。您可以创建一个视图并绘制您喜欢的任何内容,并将该视图用作表格视图单元格的背景。
【讨论】:
selectedBackgroundView 将永远不会出现。只有将“选择”设置为默认值后,selectedBackgroundView 才会出现。在 iOS 6 和 7 上测试。
cell.selectedBackgroundView = [UIView new]; 并设置您想要的任何颜色:cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHex:@"ecf2f5" andAlpha:1];
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
selectedBackgroundView 设置为使用我选择的背景颜色创建的 UIView
这对我来说效果很好。 感谢 Zonble 的提示。
【讨论】:
UITableViewCell 具有三种默认选择样式:-
实现如下:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
【讨论】:
在 Swift 中,在 cellForRowAtIndexPath 中使用它
let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView
如果您希望您的选择颜色在每个UITableViewCell 中都相同,
在AppDelegate 中使用它。
let selectedView = UIView()
selectedView.backgroundColor = .white
UITableViewCell.appearance().selectedBackgroundView = selectedView
【讨论】:
如果您想在应用范围内更改它,您可以将逻辑添加到您的 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 子类,则可以实现- (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];
selectedBackgroundView 属性。
我必须将选择样式设置为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;
}
【讨论】:
@IBDesignable class UIDesignableTableViewCell: UITableViewCell {
@IBInspectable var selectedColor: UIColor = UIColor.clearColor() {
didSet {
selectedBackgroundView = UIView()
selectedBackgroundView?.backgroundColor = selectedColor
}
}
}
在故事板中,将 UITableViewCell 的类设置为 UIDesignableTableViewCell,在属性检查器上,您可以将单元格的选定颜色更改为任何颜色。
您可以将其用于所有单元格。 这就是您的属性检查器的外观。
【讨论】:
UIColor.clear,而不是 UIColor.clearColor()。不确定这是否也是 Swift 的一个变化,但它也可以在没有 @IBDesignable 的情况下工作。
基于@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
}
}
}
【讨论】:
Swift 3.0/4.0
如果您创建了自己的自定义单元格,则可以更改 awakeFromNib() 上所有单元格的选择颜色:
override func awakeFromNib() {
super.awakeFromNib()
let colorView = UIView()
colorView.backgroundColor = UIColor.orange.withAlphaComponent(0.4)
self.selectedBackgroundView = colorView
}
【讨论】:
斯威夫特 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。
【讨论】: