Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of UITableViewCell when selecting?

I have a menu like so:

Menu

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.

like image 851
swiftcode Avatar asked Apr 28 '13 17:04

swiftcode


People also ask

How do I change the selection color of Uitableviewcell in iOS?

you can just change the backgroundView's backgroundColor property like so: cell. selectedBackgroundView?. backgroundColor = <your color .

How do I change the color of a selected cell in Swift?

Swift 3, 4, 5 select cell background colour Next connect your cell's selectedBackgroundView Outlet to this view. You can even connect multiple cells' outlets to this one view. Show activity on this post. For a solution that works (properly) with UIAppearance for iOS 7 (and higher?)

How to change uitableview background color?

Select the Tableview. Go to the Attributes Inspector. Set the Tableview Background Colour. Set the View Background Colour.


1 Answers

iOS 6.0 and later

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

- (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]; } 
like image 67
Ayush Avatar answered Oct 12 '22 23:10

Ayush