ios 选择时如何更改 UITableViewCell 的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16265662/
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
How to change color of UITableViewCell when selecting?
提问by swiftcode
I have a menu like so:
我有一个这样的菜单:
The normal (unselected) state for each cell is an image, the selected state is also an image (that looks like the default blue one). However, I'd like to add an additional third image, so that when the user selects a cell, it briefly changes to that third color before going blue (selected).
每个单元格的正常(未选择)状态是一张图像,选中状态也是一张图像(看起来像默认的蓝色)。但是,我想添加一个额外的第三个图像,以便当用户选择一个单元格时,它会在变为蓝色(选定)之前短暂地更改为第三种颜色。
This is my code:
这是我的代码:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView setBackgroundColor:[UIColor clearColor]];
NSString *cellIdentifier = @"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
UIImage *cellBackgroundNormal = [UIImage imageNamed:@"cell_menu_normal"];
UIImage *cellBackgroundSelected = [UIImage imageNamed:@"cell_menu_selected"];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:cellBackgroundNormal];
UIImageView *cellBackgroundSelectedView = [[UIImageView alloc] initWithImage:cellBackgroundSelected];
cell.backgroundView = cellBackgroundView;
cell.selectedBackgroundView = cellBackgroundSelectedView;
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
return cell;
}
As you can see, I only have two states as of now. I don't see how I can introduce some sort of cell.hoveredBackgroundView
for the third image. If anyone can help me implement this, I'd really appreciate it.
如您所见,我现在只有两个状态。我不知道如何cell.hoveredBackgroundView
为第三张图片引入某种形式。如果有人可以帮助我实现这一点,我将不胜感激。
回答by Ayush
iOS 6.0 and later
iOS 6.0 及更高版本
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Add your Colour.
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[self setCellColor:[UIColor whiteColor] ForCell:cell]; //highlight colour
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Reset Colour.
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:cell]; //normal color
}
- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
cell.contentView.backgroundColor = color;
cell.backgroundColor = color;
}
Custom UITableViewCell
自定义 UITableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
UIView * selectedBackgroundView = [[UIView alloc] init];
[selectedBackgroundView setBackgroundColor:[UIColor colorFromHexString:@"5E6073"]]; // set color here
[self setSelectedBackgroundView:selectedBackgroundView];
}
回答by Milan Cermak
You can also use UIAppearance like this:
您还可以像这样使用 UIAppearance:
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor redColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
This will apply to all instances of UITableViewCell or any of its subclasses you might have. Just make sure the selectionStyle
property of your cell is notset to UITableViewCellSelectionStyleNone
.
这将适用于您可能拥有的 UITableViewCell 或其任何子类的所有实例。只需确保selectionStyle
您的单元格的属性未设置为UITableViewCellSelectionStyleNone
.
回答by AG1
iOS 8.0 (and later) using Swift
使用 Swift 的 iOS 8.0(及更高版本)
Swift 2
斯威夫特 2
override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.contentView.backgroundColor = UIColor.orangeColor()
cell?.backgroundColor = UIColor.orangeColor()
}
override func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.contentView.backgroundColor = UIColor.blackColor()
cell?.backgroundColor = UIColor.blackColor()
}
Swift 3
斯威夫特 3
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.contentView.backgroundColor = UIColor.orange
cell?.backgroundColor = UIColor.orange
}
override func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.contentView.backgroundColor = UIColor.black
cell?.backgroundColor = UIColor.black
}
回答by marcshilling
Easier than accepted answer:
比接受的答案更容易:
In your UITableViewCell subclass:
在您的 UITableViewCell 子类中:
In awakeFromNib
or init
:
在awakeFromNib
或init
:
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.selectionStyle = UITableViewCellSelectionStyleNone;
Then:
然后:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
self.backgroundColor = [UIColor yourHighlightColor];
}
else {
self.backgroundColor = [UIColor yourNormalColor];
}
}
回答by LondonGuy
Swift:
迅速:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let selectionColor = UIView() as UIView
selectionColor.layer.borderWidth = 1
selectionColor.layer.borderColor = UIColor.blueColor().CGColor
selectionColor.backgroundColor = UIColor.blueColor()
cell.selectedBackgroundView = selectionColor
}
Swift 4:
斯威夫特 4:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
let selectionColor = UIView() as UIView
selectionColor.layer.borderWidth = 1
selectionColor.layer.borderColor = UIColor.blue.cgColor
selectionColor.backgroundColor = UIColor.blue
cell.selectedBackgroundView = selectionColor
}
回答by Zoeb S
Try this in custom cell-
在自定义单元格中试试这个 -
- (void)awakeFromNib
{
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
selectedBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
selectedBackgroundView.backgroundColor = [UIColor colorWithRed:246.0/255.0 green:95.0/255.0 blue:22.0/255.0 alpha:1.0];
self.selectedBackgroundView = selectedBackgroundView;
}
回答by Amro Shafie
Based on Milan Cermak's answer, you can use UIAppearance
.
根据 Milan Cermak 的回答,您可以使用UIAppearance
.
In Swift 1.1 / 2.0
:
在Swift 1.1 / 2.0
:
let selectionView = UIView()
selectionView.backgroundColor = UIColor.redColor()
UITableViewCell.appearance().selectedBackgroundView = selectionView
回答by ViJay Avhad
Using Objective C, Change Selected Cell Background color other than defaults. No need to create custom cells.
使用目标 C,更改选定单元格背景颜色而不是默认值。无需创建自定义单元格。
If you only want to change the selected color of the cell, you can do this, Be aware that for this to work, in the Storyboard (or XIB file) you must select a Selected Background colour other than None. Just add following code in UITableView Delegate method : tableView cellForRowAtIndexPath:
如果您只想更改单元格的选定颜色,您可以这样做,请注意,要使其正常工作,在 Storyboard(或 XIB 文件)中,您必须选择“无”以外的选定背景颜色。只需在 UITableView Delegate 方法中添加以下代码:tableView cellForRowAtIndexPath:
UIView *bgColor = [[UIView alloc] init];
bgColor.backgroundColor = [UIColor yellowColor];
[cell setSelectedBackgroundView:bgColor];
回答by Kiran Patel
Custom UITableViewCell Swift 3.0
自定义 UITableViewCell Swift 3.0
override func awakeFromNib() {
super.awakeFromNib()
let selectedBackgroundView = UIView();
selectedBackgroundView.backgroundColor = UIColor.lightGray;
self.selectedBackgroundView = selectedBackgroundView;
}
回答by Avijit Nagare
I end up with following code.
我最终得到以下代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[cellIdArray objectAtIndex:indexPath.row] forIndexPath:indexPath];
// Configure the cell...
cell.backgroundView =
[[UIImageView alloc] init] ;
cell.selectedBackgroundView =[[UIImageView alloc] init];
UIImage *rowBackground;
UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:@"cellBackgroundDarkGrey.png"];
selectionBackground = [UIImage imageNamed:@"selectedMenu.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
return cell;
}