I have a menu like so:
The normal (unselected) state for each cell is an image, the selected s
Easier than accepted answer:
In your UITableViewCell subclass:
In awakeFromNib
or init
:
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];
}
}
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];
}
You could introduce a timer to set the time you want (like 1/2 s if I understood) between the 2 different background colors ? But you may already thought about that :/
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;
}
For swift 3
self.tableView.reloadData()
let selectedCell = tableView.cellForRow(at: indexPath)
selectedCell?.contentView.backgroundColor = UIColor.red
Custom UITableViewCell Swift 3.0
override func awakeFromNib() {
super.awakeFromNib()
let selectedBackgroundView = UIView();
selectedBackgroundView.backgroundColor = UIColor.lightGray;
self.selectedBackgroundView = selectedBackgroundView;
}