I have a menu like so:

The normal (unselected) state for each cell is an image, the selected s
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;
}
Add following code to cellForRowAtIndexPath method
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
You could create a custom UITableViewCell, in which you add a UIButton with the size of the cell. Then you can easily make custom methods for the UIButton's TouchDown (hover) and TouchUpInside events, and set the background.
- (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;
}
Based on Milan Cermak's answer, you can use UIAppearance.
In Swift 1.1 / 2.0:
let selectionView = UIView()
selectionView.backgroundColor = UIColor.redColor()
UITableViewCell.appearance().selectedBackgroundView = selectionView
You can also use UIAppearance like this:
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 not set to UITableViewCellSelectionStyleNone.
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:
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
}