如何在select时更改UITableViewCell的颜色?
我有一个像这样的菜单:
每个单元的正常(未选定)状态是一个图像,所选状态也是一个图像(看起来像默认的蓝色)。 但是,我想添加一个额外的第三个图像,以便当用户select一个单元格时,在进行蓝色(选定)之前,它会短暂更改为第三个颜色。
这是我的代码:
(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; }
正如你所看到的,截至目前我只有两个国家。 我没有看到我可以为第三张图片引入某种cell.hoveredBackgroundView
。 如果有人能帮我实施这个,我真的很感激。
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; }
自定义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]; }
你也可以像这样使用UIAppearance:
UIView *selectionView = [UIView new]; selectionView.backgroundColor = [UIColor redColor]; [[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
这将适用于UITableViewCell或其任何子类的所有实例。 只要确保你的单元格的selectionStyle
属性没有设置为UITableViewCellSelectionStyleNone
。
iOS 8.0(及更高版本)使用Swift
Swift 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
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 }
比接受的答案更容易:
在你的UITableViewCell子类中:
在awakeFromNib
或init
:
self.selectionStyle = UITableViewCellSelectionStyleNone;
然后:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [super setHighlighted:highlighted animated:animated]; if (highlighted) { self.backgroundColor = [UIColor yourHighlightColor]; } else { self.backgroundColor = [UIColor yourNormalColor]; } }
尝试在自定义单元格中,
- (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; }
迅速:
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 }
基于米兰Cermak的答案,你可以使用UIAppearance
。
在Swift 1.1 / 2.0
:
let selectionView = UIView() selectionView.backgroundColor = UIColor.redColor() UITableViewCell.appearance().selectedBackgroundView = selectionView
使用Objective C,更改选定的单元格背景颜色,而不是默认值。 无需创build自定义单元格。
如果您只想更改单元格的选定颜色,则可以这样做,请注意,要使其工作,请在“故事板”(或XIB文件)中select“无”以外的选定背景颜色。 只需在UITableView委托方法中添加以下代码:tableView cellForRowAtIndexPath:
UIView *bgColor = [[UIView alloc] init]; bgColor.backgroundColor = [UIColor yellowColor]; [cell setSelectedBackgroundView:bgColor];
自定义的UITableViewCell Swift 3.0
override func awakeFromNib() { super.awakeFromNib() let selectedBackgroundView = UIView(); selectedBackgroundView.backgroundColor = UIColor.lightGray; self.selectedBackgroundView = selectedBackgroundView; }
将以下代码添加到cellForRowAtIndexPath方法
var cell=tableView.dequeueReusableCellWithIdentifier("cell")! var viewBG=UIView(frame: CGRectMake(0,0,self.view.frame.size.width,50)) viewBG.backgroundColor=UIColor(colorLiteralRed: 71.0/255.0, green: 121.0/255.0, blue: 172.0/255.0, alpha: 1) cell.selectedBackgroundView=viewBG
快3
self.tableView.reloadData() let selectedCell = tableView.cellForRow(at: indexPath) selectedCell?.contentView.backgroundColor = UIColor.red
我最终以下面的代码。
- (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; }
你可以创build一个自定义的UITableViewCell,在其中添加一个单元格大小的UIButton。 然后,您可以轻松地为UIButton的TouchDown(hover)和TouchUpInside事件创build自定义方法,并设置背景。
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { cellButton = [[UIButton alloc] initWithFrame:self.contentView.frame]; UIImage *cellBackgroundNormal = [UIImage imageNamed:@"cell_menu_normal"]; UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:cellBackgroundNormal]; self.backgroundView = cellBackgroundView; [cellButton addTarget:self action:@selector(hoverCell) forControlEvents:UIControlEventTouchDown]; [cellButton addTarget:self action:@selector(tapCell) forControlEvents:UIControlEventTouchUpInside]; } return self; } - (void)hoverCell { UIImage *cellBackgroundHover = [UIImage imageNamed:@"cell_menu_third_image"]; UIImageView *cellBackgroundHoverView = [[UIImageView alloc] initWithImage:cellBackgroundHover]; self.backgroundView = cellBackgroundHoverView; } - (void)tapCell { UIImage *cellBackgroundSelected = [UIImage imageNamed:@"cell_menu_selected"]; UIImageView *cellBackgroundSelectedView = [[UIImageView alloc] initWithImage:cellBackgroundSelected]; self.backgroundView = cellBackgroundSelectedView; }
你可以引入一个计时器来设置两种不同的背景颜色之间的时间(如果我理解的话,就是1/2秒)? 但你可能已经想过:/