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.
you can just change the backgroundView's backgroundColor property like so: cell. selectedBackgroundView?. backgroundColor = <your color .
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?)
Select the Tableview. Go to the Attributes Inspector. Set the Tableview Background Colour. Set the View Background Colour.
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]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With