disable the uitableview highlighting but allow the selection of individual cells

IosObjective CUitableview

Ios Problem Overview


when you tap on a cell the row gets selected and highlighted.Now what i want to do is disable the highlighting but allow the selection.Is there a way around it.There is question that answers this but it disables both the selection and highlighting.

Ios Solutions


Solution 1 - Ios

You can just set the cell's selection style to "None" from Storyboard:

See here

Or from code:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

For Swift 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none

For Swift 4 & above:

cell.selectionStyle = .none

Solution 2 - Ios

Change UITableViewCell's selectedBackgroundView color to transparent.

    let clearView = UIView()
    clearView.backgroundColor = UIColor.clearColor() // Whatever color you like
    UITableViewCell.appearance().selectedBackgroundView = clearView

or to set for a specific cell:

cell.backgroundView = clearView

Solution 3 - Ios

Try setting cell selection style to None -

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

This will solve your problem

Solution 4 - Ios

For Swift 3.0:

cell.selectionStyle = .none

Solution 5 - Ios

For Swift:

UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;

Or when you subclass a cell in swift:

class CustomCell : UITableViewCell {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        selectionStyle = .None
    }

}

Solution 6 - Ios

in swift 3

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
}

Solution 7 - Ios

To add a custom color use the below code. And to make it transparent use alpha: 0.0

cell.selectedBackgroundView = UIView(frame: CGRect.zero)
cell.selectedBackgroundView?.backgroundColor = UIColor(red:0.27, green:0.71, blue:0.73, alpha:1.0)

If you use custom color and want to give it rounded corner look use:

cell.layer.cornerRadius = 8

Also, use this for better animation and feel

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    tableView.deselectRow(at: indexPath, animated: true)
}

Solution 8 - Ios

For those looking for a programmatic way in Swift 3

cell.selectionStyle = UITableViewCellSelectionStyle.none

Solution 9 - Ios

You can set the selection property of the cell itself in the storyboard enter image description here

Solution 10 - Ios

For Swift 5 the best way is:

cell.selectionStyle = .none

Solution 11 - Ios

For Objc:

> [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

- (void)viewDidLoad {
  [super viewDidLoad];
  _tableView.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .. .. .. .. 
   [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
   . . . . .. 
}

Solution 12 - Ios

Here is the solution for swift 3,works even in editing mode

cell.selectionStyle = .gray
cell.selectedBackgroundView = {
    
    let colorView = UIView()
    colorView.backgroundColor = UIColor.black.withAlphaComponent(0.0)
    //change the alpha value or color to match with you UI/UX
    
    return colorView
}()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsoldiershinView Question on Stackoverflow
Solution 1 - IosJózsef VeszaView Answer on Stackoverflow
Solution 2 - IostounaobunView Answer on Stackoverflow
Solution 3 - IosViperView Answer on Stackoverflow
Solution 4 - IosElikoView Answer on Stackoverflow
Solution 5 - IosMelvinView Answer on Stackoverflow
Solution 6 - IosSai kumar ReddyView Answer on Stackoverflow
Solution 7 - IosPrateekroView Answer on Stackoverflow
Solution 8 - IosDanoramView Answer on Stackoverflow
Solution 9 - IosM.EView Answer on Stackoverflow
Solution 10 - IosramzesenokView Answer on Stackoverflow
Solution 11 - IosLakhdeep SinghView Answer on Stackoverflow
Solution 12 - IosDukeXView Answer on Stackoverflow